Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/TESTS/mbed_drivers/ticker/main.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2013-2017 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 5 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 6 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 7 | * |
marcozecchini | 0:9fca2b23d0ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 9 | * |
marcozecchini | 0:9fca2b23d0ba | 10 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 13 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 14 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 15 | */ |
marcozecchini | 0:9fca2b23d0ba | 16 | #include "mbed.h" |
marcozecchini | 0:9fca2b23d0ba | 17 | #include "greentea-client/test_env.h" |
marcozecchini | 0:9fca2b23d0ba | 18 | #include "utest/utest.h" |
marcozecchini | 0:9fca2b23d0ba | 19 | #include "unity/unity.h" |
marcozecchini | 0:9fca2b23d0ba | 20 | |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | using utest::v1::Case; |
marcozecchini | 0:9fca2b23d0ba | 23 | |
marcozecchini | 0:9fca2b23d0ba | 24 | #define ONE_MILLI_SEC 1000 |
marcozecchini | 0:9fca2b23d0ba | 25 | #define TICKER_COUNT 16 |
marcozecchini | 0:9fca2b23d0ba | 26 | #define MULTI_TICKER_TIME_MS 100 |
marcozecchini | 0:9fca2b23d0ba | 27 | volatile uint32_t callback_trigger_count = 0; |
marcozecchini | 0:9fca2b23d0ba | 28 | static const int test_timeout = 240; |
marcozecchini | 0:9fca2b23d0ba | 29 | static const int total_ticks = 10; |
marcozecchini | 0:9fca2b23d0ba | 30 | |
marcozecchini | 0:9fca2b23d0ba | 31 | |
marcozecchini | 0:9fca2b23d0ba | 32 | /* Tolerance is quite arbitrary due to large number of boards with varying level of accuracy */ |
marcozecchini | 0:9fca2b23d0ba | 33 | #define TOLERANCE_US 1000 |
marcozecchini | 0:9fca2b23d0ba | 34 | |
marcozecchini | 0:9fca2b23d0ba | 35 | volatile uint32_t ticker_callback_flag; |
marcozecchini | 0:9fca2b23d0ba | 36 | volatile uint32_t multi_counter; |
marcozecchini | 0:9fca2b23d0ba | 37 | |
marcozecchini | 0:9fca2b23d0ba | 38 | DigitalOut led1(LED1); |
marcozecchini | 0:9fca2b23d0ba | 39 | DigitalOut led2(LED2); |
marcozecchini | 0:9fca2b23d0ba | 40 | |
marcozecchini | 0:9fca2b23d0ba | 41 | Ticker *volatile ticker1; |
marcozecchini | 0:9fca2b23d0ba | 42 | Ticker *volatile ticker2; |
marcozecchini | 0:9fca2b23d0ba | 43 | Timer gtimer; |
marcozecchini | 0:9fca2b23d0ba | 44 | |
marcozecchini | 0:9fca2b23d0ba | 45 | volatile int ticker_count = 0; |
marcozecchini | 0:9fca2b23d0ba | 46 | volatile bool print_tick = false; |
marcozecchini | 0:9fca2b23d0ba | 47 | |
marcozecchini | 0:9fca2b23d0ba | 48 | void ticker_callback_1_switch_to_2(void); |
marcozecchini | 0:9fca2b23d0ba | 49 | void ticker_callback_2_switch_to_1(void); |
marcozecchini | 0:9fca2b23d0ba | 50 | |
marcozecchini | 0:9fca2b23d0ba | 51 | void increment_ticker_counter(void) |
marcozecchini | 0:9fca2b23d0ba | 52 | { |
marcozecchini | 0:9fca2b23d0ba | 53 | ++callback_trigger_count; |
marcozecchini | 0:9fca2b23d0ba | 54 | } |
marcozecchini | 0:9fca2b23d0ba | 55 | |
marcozecchini | 0:9fca2b23d0ba | 56 | void switch_led1_state(void) |
marcozecchini | 0:9fca2b23d0ba | 57 | { |
marcozecchini | 0:9fca2b23d0ba | 58 | led1 = !led1; |
marcozecchini | 0:9fca2b23d0ba | 59 | } |
marcozecchini | 0:9fca2b23d0ba | 60 | |
marcozecchini | 0:9fca2b23d0ba | 61 | void switch_led2_state(void) |
marcozecchini | 0:9fca2b23d0ba | 62 | { |
marcozecchini | 0:9fca2b23d0ba | 63 | led2 = !led2; |
marcozecchini | 0:9fca2b23d0ba | 64 | } |
marcozecchini | 0:9fca2b23d0ba | 65 | |
marcozecchini | 0:9fca2b23d0ba | 66 | void ticker_callback_1_switch_to_2(void) |
marcozecchini | 0:9fca2b23d0ba | 67 | { |
marcozecchini | 0:9fca2b23d0ba | 68 | ++callback_trigger_count; |
marcozecchini | 0:9fca2b23d0ba | 69 | // If ticker is NULL then it is being or has been deleted |
marcozecchini | 0:9fca2b23d0ba | 70 | if (ticker1) { |
marcozecchini | 0:9fca2b23d0ba | 71 | ticker1->detach(); |
marcozecchini | 0:9fca2b23d0ba | 72 | ticker1->attach_us(ticker_callback_2_switch_to_1, ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 73 | } |
marcozecchini | 0:9fca2b23d0ba | 74 | switch_led1_state(); |
marcozecchini | 0:9fca2b23d0ba | 75 | } |
marcozecchini | 0:9fca2b23d0ba | 76 | |
marcozecchini | 0:9fca2b23d0ba | 77 | void ticker_callback_2_switch_to_1(void) |
marcozecchini | 0:9fca2b23d0ba | 78 | { |
marcozecchini | 0:9fca2b23d0ba | 79 | ++callback_trigger_count; |
marcozecchini | 0:9fca2b23d0ba | 80 | // If ticker is NULL then it is being or has been deleted |
marcozecchini | 0:9fca2b23d0ba | 81 | if (ticker2) { |
marcozecchini | 0:9fca2b23d0ba | 82 | ticker2->detach(); |
marcozecchini | 0:9fca2b23d0ba | 83 | ticker2->attach_us(ticker_callback_1_switch_to_2, ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 84 | } |
marcozecchini | 0:9fca2b23d0ba | 85 | switch_led2_state(); |
marcozecchini | 0:9fca2b23d0ba | 86 | } |
marcozecchini | 0:9fca2b23d0ba | 87 | |
marcozecchini | 0:9fca2b23d0ba | 88 | |
marcozecchini | 0:9fca2b23d0ba | 89 | void sem_release(Semaphore *sem) |
marcozecchini | 0:9fca2b23d0ba | 90 | { |
marcozecchini | 0:9fca2b23d0ba | 91 | sem->release(); |
marcozecchini | 0:9fca2b23d0ba | 92 | } |
marcozecchini | 0:9fca2b23d0ba | 93 | |
marcozecchini | 0:9fca2b23d0ba | 94 | |
marcozecchini | 0:9fca2b23d0ba | 95 | void stop_gtimer_set_flag(void) |
marcozecchini | 0:9fca2b23d0ba | 96 | { |
marcozecchini | 0:9fca2b23d0ba | 97 | gtimer.stop(); |
marcozecchini | 0:9fca2b23d0ba | 98 | core_util_atomic_incr_u32((uint32_t*)&ticker_callback_flag, 1); |
marcozecchini | 0:9fca2b23d0ba | 99 | } |
marcozecchini | 0:9fca2b23d0ba | 100 | |
marcozecchini | 0:9fca2b23d0ba | 101 | void increment_multi_counter(void) |
marcozecchini | 0:9fca2b23d0ba | 102 | { |
marcozecchini | 0:9fca2b23d0ba | 103 | core_util_atomic_incr_u32((uint32_t*)&multi_counter, 1); |
marcozecchini | 0:9fca2b23d0ba | 104 | } |
marcozecchini | 0:9fca2b23d0ba | 105 | |
marcozecchini | 0:9fca2b23d0ba | 106 | |
marcozecchini | 0:9fca2b23d0ba | 107 | /* Tests is to measure the accuracy of Ticker over a period of time |
marcozecchini | 0:9fca2b23d0ba | 108 | * |
marcozecchini | 0:9fca2b23d0ba | 109 | * 1) DUT would start to update callback_trigger_count every milli sec, in 2x callback we use 2 tickers |
marcozecchini | 0:9fca2b23d0ba | 110 | * to update the count alternatively. |
marcozecchini | 0:9fca2b23d0ba | 111 | * 2) Host would query what is current count base_time, Device responds by the callback_trigger_count |
marcozecchini | 0:9fca2b23d0ba | 112 | * 3) Host after waiting for measurement stretch. It will query for device time again final_time. |
marcozecchini | 0:9fca2b23d0ba | 113 | * 4) Host computes the drift considering base_time, final_time, transport delay and measurement stretch |
marcozecchini | 0:9fca2b23d0ba | 114 | * 5) Finally host send the results back to device pass/fail based on tolerance. |
marcozecchini | 0:9fca2b23d0ba | 115 | * 6) More details on tests can be found in timing_drift_auto.py |
marcozecchini | 0:9fca2b23d0ba | 116 | */ |
marcozecchini | 0:9fca2b23d0ba | 117 | void test_case_1x_ticker() |
marcozecchini | 0:9fca2b23d0ba | 118 | { |
marcozecchini | 0:9fca2b23d0ba | 119 | char _key[11] = { }; |
marcozecchini | 0:9fca2b23d0ba | 120 | char _value[128] = { }; |
marcozecchini | 0:9fca2b23d0ba | 121 | int expected_key = 1; |
marcozecchini | 0:9fca2b23d0ba | 122 | |
marcozecchini | 0:9fca2b23d0ba | 123 | greentea_send_kv("timing_drift_check_start", 0); |
marcozecchini | 0:9fca2b23d0ba | 124 | ticker1->attach_us(&increment_ticker_counter, ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 125 | |
marcozecchini | 0:9fca2b23d0ba | 126 | // wait for 1st signal from host |
marcozecchini | 0:9fca2b23d0ba | 127 | do { |
marcozecchini | 0:9fca2b23d0ba | 128 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
marcozecchini | 0:9fca2b23d0ba | 129 | expected_key = strcmp(_key, "base_time"); |
marcozecchini | 0:9fca2b23d0ba | 130 | } while (expected_key); |
marcozecchini | 0:9fca2b23d0ba | 131 | greentea_send_kv(_key, callback_trigger_count * ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 132 | |
marcozecchini | 0:9fca2b23d0ba | 133 | // wait for 2nd signal from host |
marcozecchini | 0:9fca2b23d0ba | 134 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
marcozecchini | 0:9fca2b23d0ba | 135 | greentea_send_kv(_key, callback_trigger_count * ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 136 | |
marcozecchini | 0:9fca2b23d0ba | 137 | //get the results from host |
marcozecchini | 0:9fca2b23d0ba | 138 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
marcozecchini | 0:9fca2b23d0ba | 139 | |
marcozecchini | 0:9fca2b23d0ba | 140 | TEST_ASSERT_EQUAL_STRING_MESSAGE("pass", _key,"Host side script reported a fail..."); |
marcozecchini | 0:9fca2b23d0ba | 141 | } |
marcozecchini | 0:9fca2b23d0ba | 142 | |
marcozecchini | 0:9fca2b23d0ba | 143 | void test_case_2x_callbacks() |
marcozecchini | 0:9fca2b23d0ba | 144 | { |
marcozecchini | 0:9fca2b23d0ba | 145 | char _key[11] = { }; |
marcozecchini | 0:9fca2b23d0ba | 146 | char _value[128] = { }; |
marcozecchini | 0:9fca2b23d0ba | 147 | int expected_key = 1; |
marcozecchini | 0:9fca2b23d0ba | 148 | |
marcozecchini | 0:9fca2b23d0ba | 149 | led1 = 0; |
marcozecchini | 0:9fca2b23d0ba | 150 | led2 = 0; |
marcozecchini | 0:9fca2b23d0ba | 151 | callback_trigger_count = 0; |
marcozecchini | 0:9fca2b23d0ba | 152 | |
marcozecchini | 0:9fca2b23d0ba | 153 | greentea_send_kv("timing_drift_check_start", 0); |
marcozecchini | 0:9fca2b23d0ba | 154 | ticker1->attach_us(ticker_callback_1_switch_to_2, ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 155 | |
marcozecchini | 0:9fca2b23d0ba | 156 | // wait for 1st signal from host |
marcozecchini | 0:9fca2b23d0ba | 157 | do { |
marcozecchini | 0:9fca2b23d0ba | 158 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
marcozecchini | 0:9fca2b23d0ba | 159 | expected_key = strcmp(_key, "base_time"); |
marcozecchini | 0:9fca2b23d0ba | 160 | } while (expected_key); |
marcozecchini | 0:9fca2b23d0ba | 161 | greentea_send_kv(_key, callback_trigger_count * ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 162 | |
marcozecchini | 0:9fca2b23d0ba | 163 | // wait for 2nd signal from host |
marcozecchini | 0:9fca2b23d0ba | 164 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
marcozecchini | 0:9fca2b23d0ba | 165 | greentea_send_kv(_key, callback_trigger_count * ONE_MILLI_SEC); |
marcozecchini | 0:9fca2b23d0ba | 166 | |
marcozecchini | 0:9fca2b23d0ba | 167 | //get the results from host |
marcozecchini | 0:9fca2b23d0ba | 168 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
marcozecchini | 0:9fca2b23d0ba | 169 | |
marcozecchini | 0:9fca2b23d0ba | 170 | TEST_ASSERT_EQUAL_STRING_MESSAGE("pass", _key,"Host side script reported a fail..."); |
marcozecchini | 0:9fca2b23d0ba | 171 | } |
marcozecchini | 0:9fca2b23d0ba | 172 | |
marcozecchini | 0:9fca2b23d0ba | 173 | /** Test many tickers run one after the other |
marcozecchini | 0:9fca2b23d0ba | 174 | |
marcozecchini | 0:9fca2b23d0ba | 175 | Given many Tickers |
marcozecchini | 0:9fca2b23d0ba | 176 | When schedule them one after the other with the same time intervals |
marcozecchini | 0:9fca2b23d0ba | 177 | Then tickers properly execute callbacks |
marcozecchini | 0:9fca2b23d0ba | 178 | When schedule them one after the other with the different time intervals |
marcozecchini | 0:9fca2b23d0ba | 179 | Then tickers properly execute callbacks |
marcozecchini | 0:9fca2b23d0ba | 180 | */ |
marcozecchini | 0:9fca2b23d0ba | 181 | void test_multi_ticker(void) |
marcozecchini | 0:9fca2b23d0ba | 182 | { |
marcozecchini | 0:9fca2b23d0ba | 183 | Ticker ticker[TICKER_COUNT]; |
marcozecchini | 0:9fca2b23d0ba | 184 | const uint32_t extra_wait = 5; // extra 5ms wait time |
marcozecchini | 0:9fca2b23d0ba | 185 | |
marcozecchini | 0:9fca2b23d0ba | 186 | multi_counter = 0; |
marcozecchini | 0:9fca2b23d0ba | 187 | for (int i = 0; i < TICKER_COUNT; i++) { |
marcozecchini | 0:9fca2b23d0ba | 188 | ticker[i].attach_us(callback(increment_multi_counter), MULTI_TICKER_TIME_MS * 1000); |
marcozecchini | 0:9fca2b23d0ba | 189 | } |
marcozecchini | 0:9fca2b23d0ba | 190 | |
marcozecchini | 0:9fca2b23d0ba | 191 | Thread::wait(MULTI_TICKER_TIME_MS + extra_wait); |
marcozecchini | 0:9fca2b23d0ba | 192 | for (int i = 0; i < TICKER_COUNT; i++) { |
marcozecchini | 0:9fca2b23d0ba | 193 | ticker[i].detach(); |
marcozecchini | 0:9fca2b23d0ba | 194 | } |
marcozecchini | 0:9fca2b23d0ba | 195 | TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter); |
marcozecchini | 0:9fca2b23d0ba | 196 | |
marcozecchini | 0:9fca2b23d0ba | 197 | multi_counter = 0; |
marcozecchini | 0:9fca2b23d0ba | 198 | for (int i = 0; i < TICKER_COUNT; i++) { |
marcozecchini | 0:9fca2b23d0ba | 199 | ticker[i].attach_us(callback(increment_multi_counter), (MULTI_TICKER_TIME_MS + i) * 1000); |
marcozecchini | 0:9fca2b23d0ba | 200 | } |
marcozecchini | 0:9fca2b23d0ba | 201 | |
marcozecchini | 0:9fca2b23d0ba | 202 | Thread::wait(MULTI_TICKER_TIME_MS + TICKER_COUNT + extra_wait); |
marcozecchini | 0:9fca2b23d0ba | 203 | for (int i = 0; i < TICKER_COUNT; i++) { |
marcozecchini | 0:9fca2b23d0ba | 204 | ticker[i].detach(); |
marcozecchini | 0:9fca2b23d0ba | 205 | } |
marcozecchini | 0:9fca2b23d0ba | 206 | TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter); |
marcozecchini | 0:9fca2b23d0ba | 207 | } |
marcozecchini | 0:9fca2b23d0ba | 208 | |
marcozecchini | 0:9fca2b23d0ba | 209 | /** Test multi callback time |
marcozecchini | 0:9fca2b23d0ba | 210 | |
marcozecchini | 0:9fca2b23d0ba | 211 | Given a Ticker |
marcozecchini | 0:9fca2b23d0ba | 212 | When the callback is attached multiple times |
marcozecchini | 0:9fca2b23d0ba | 213 | Then ticker properly execute callback multiple times |
marcozecchini | 0:9fca2b23d0ba | 214 | */ |
marcozecchini | 0:9fca2b23d0ba | 215 | void test_multi_call_time(void) |
marcozecchini | 0:9fca2b23d0ba | 216 | { |
marcozecchini | 0:9fca2b23d0ba | 217 | Ticker ticker; |
marcozecchini | 0:9fca2b23d0ba | 218 | int time_diff; |
marcozecchini | 0:9fca2b23d0ba | 219 | const int attach_count = 10; |
marcozecchini | 0:9fca2b23d0ba | 220 | |
marcozecchini | 0:9fca2b23d0ba | 221 | for (int i = 0; i < attach_count; i++) { |
marcozecchini | 0:9fca2b23d0ba | 222 | ticker_callback_flag = 0; |
marcozecchini | 0:9fca2b23d0ba | 223 | gtimer.reset(); |
marcozecchini | 0:9fca2b23d0ba | 224 | |
marcozecchini | 0:9fca2b23d0ba | 225 | gtimer.start(); |
marcozecchini | 0:9fca2b23d0ba | 226 | ticker.attach_us(callback(stop_gtimer_set_flag), MULTI_TICKER_TIME_MS * 1000); |
marcozecchini | 0:9fca2b23d0ba | 227 | while(!ticker_callback_flag); |
marcozecchini | 0:9fca2b23d0ba | 228 | time_diff = gtimer.read_us(); |
marcozecchini | 0:9fca2b23d0ba | 229 | |
marcozecchini | 0:9fca2b23d0ba | 230 | TEST_ASSERT_UINT32_WITHIN(TOLERANCE_US, MULTI_TICKER_TIME_MS * 1000, time_diff); |
marcozecchini | 0:9fca2b23d0ba | 231 | } |
marcozecchini | 0:9fca2b23d0ba | 232 | } |
marcozecchini | 0:9fca2b23d0ba | 233 | |
marcozecchini | 0:9fca2b23d0ba | 234 | /** Test if detach cancel scheduled callback event |
marcozecchini | 0:9fca2b23d0ba | 235 | |
marcozecchini | 0:9fca2b23d0ba | 236 | Given a Ticker with callback attached |
marcozecchini | 0:9fca2b23d0ba | 237 | When the callback is detached |
marcozecchini | 0:9fca2b23d0ba | 238 | Then the callback is not being called |
marcozecchini | 0:9fca2b23d0ba | 239 | */ |
marcozecchini | 0:9fca2b23d0ba | 240 | void test_detach(void) |
marcozecchini | 0:9fca2b23d0ba | 241 | { |
marcozecchini | 0:9fca2b23d0ba | 242 | Ticker ticker; |
marcozecchini | 0:9fca2b23d0ba | 243 | int32_t ret; |
marcozecchini | 0:9fca2b23d0ba | 244 | const float ticker_time_s = 0.1f; |
marcozecchini | 0:9fca2b23d0ba | 245 | const uint32_t wait_time_ms = 500; |
marcozecchini | 0:9fca2b23d0ba | 246 | Semaphore sem(0, 1); |
marcozecchini | 0:9fca2b23d0ba | 247 | |
marcozecchini | 0:9fca2b23d0ba | 248 | ticker.attach(callback(sem_release, &sem), ticker_time_s); |
marcozecchini | 0:9fca2b23d0ba | 249 | |
marcozecchini | 0:9fca2b23d0ba | 250 | ret = sem.wait(); |
marcozecchini | 0:9fca2b23d0ba | 251 | TEST_ASSERT_TRUE(ret > 0); |
marcozecchini | 0:9fca2b23d0ba | 252 | |
marcozecchini | 0:9fca2b23d0ba | 253 | ret = sem.wait(); |
marcozecchini | 0:9fca2b23d0ba | 254 | ticker.detach(); /* cancel */ |
marcozecchini | 0:9fca2b23d0ba | 255 | TEST_ASSERT_TRUE(ret > 0); |
marcozecchini | 0:9fca2b23d0ba | 256 | |
marcozecchini | 0:9fca2b23d0ba | 257 | ret = sem.wait(wait_time_ms); |
marcozecchini | 0:9fca2b23d0ba | 258 | TEST_ASSERT_EQUAL(0, ret); |
marcozecchini | 0:9fca2b23d0ba | 259 | } |
marcozecchini | 0:9fca2b23d0ba | 260 | |
marcozecchini | 0:9fca2b23d0ba | 261 | /** Test single callback time via attach |
marcozecchini | 0:9fca2b23d0ba | 262 | |
marcozecchini | 0:9fca2b23d0ba | 263 | Given a Ticker |
marcozecchini | 0:9fca2b23d0ba | 264 | When callback attached with time interval specified |
marcozecchini | 0:9fca2b23d0ba | 265 | Then ticker properly executes callback within a specified time interval |
marcozecchini | 0:9fca2b23d0ba | 266 | */ |
marcozecchini | 0:9fca2b23d0ba | 267 | template<us_timestamp_t DELAY_US> |
marcozecchini | 0:9fca2b23d0ba | 268 | void test_attach_time(void) |
marcozecchini | 0:9fca2b23d0ba | 269 | { |
marcozecchini | 0:9fca2b23d0ba | 270 | Ticker ticker; |
marcozecchini | 0:9fca2b23d0ba | 271 | ticker_callback_flag = 0; |
marcozecchini | 0:9fca2b23d0ba | 272 | |
marcozecchini | 0:9fca2b23d0ba | 273 | gtimer.reset(); |
marcozecchini | 0:9fca2b23d0ba | 274 | gtimer.start(); |
marcozecchini | 0:9fca2b23d0ba | 275 | ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f); |
marcozecchini | 0:9fca2b23d0ba | 276 | while(!ticker_callback_flag); |
marcozecchini | 0:9fca2b23d0ba | 277 | ticker.detach(); |
marcozecchini | 0:9fca2b23d0ba | 278 | const int time_diff = gtimer.read_us(); |
marcozecchini | 0:9fca2b23d0ba | 279 | |
marcozecchini | 0:9fca2b23d0ba | 280 | TEST_ASSERT_UINT64_WITHIN(TOLERANCE_US, DELAY_US, time_diff); |
marcozecchini | 0:9fca2b23d0ba | 281 | } |
marcozecchini | 0:9fca2b23d0ba | 282 | |
marcozecchini | 0:9fca2b23d0ba | 283 | /** Test single callback time via attach_us |
marcozecchini | 0:9fca2b23d0ba | 284 | |
marcozecchini | 0:9fca2b23d0ba | 285 | Given a Ticker |
marcozecchini | 0:9fca2b23d0ba | 286 | When callback attached with time interval specified |
marcozecchini | 0:9fca2b23d0ba | 287 | Then ticker properly executes callback within a specified time interval |
marcozecchini | 0:9fca2b23d0ba | 288 | */ |
marcozecchini | 0:9fca2b23d0ba | 289 | template<us_timestamp_t DELAY_US> |
marcozecchini | 0:9fca2b23d0ba | 290 | void test_attach_us_time(void) |
marcozecchini | 0:9fca2b23d0ba | 291 | { |
marcozecchini | 0:9fca2b23d0ba | 292 | Ticker ticker; |
marcozecchini | 0:9fca2b23d0ba | 293 | ticker_callback_flag = 0; |
marcozecchini | 0:9fca2b23d0ba | 294 | |
marcozecchini | 0:9fca2b23d0ba | 295 | gtimer.reset(); |
marcozecchini | 0:9fca2b23d0ba | 296 | gtimer.start(); |
marcozecchini | 0:9fca2b23d0ba | 297 | ticker.attach_us(callback(stop_gtimer_set_flag), DELAY_US); |
marcozecchini | 0:9fca2b23d0ba | 298 | while(!ticker_callback_flag); |
marcozecchini | 0:9fca2b23d0ba | 299 | ticker.detach(); |
marcozecchini | 0:9fca2b23d0ba | 300 | const int time_diff = gtimer.read_us(); |
marcozecchini | 0:9fca2b23d0ba | 301 | |
marcozecchini | 0:9fca2b23d0ba | 302 | TEST_ASSERT_UINT64_WITHIN(TOLERANCE_US, DELAY_US, time_diff); |
marcozecchini | 0:9fca2b23d0ba | 303 | } |
marcozecchini | 0:9fca2b23d0ba | 304 | |
marcozecchini | 0:9fca2b23d0ba | 305 | |
marcozecchini | 0:9fca2b23d0ba | 306 | utest::v1::status_t one_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) |
marcozecchini | 0:9fca2b23d0ba | 307 | { |
marcozecchini | 0:9fca2b23d0ba | 308 | ticker1 = new Ticker(); |
marcozecchini | 0:9fca2b23d0ba | 309 | return greentea_case_setup_handler(source, index_of_case); |
marcozecchini | 0:9fca2b23d0ba | 310 | } |
marcozecchini | 0:9fca2b23d0ba | 311 | |
marcozecchini | 0:9fca2b23d0ba | 312 | utest::v1::status_t two_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) |
marcozecchini | 0:9fca2b23d0ba | 313 | { |
marcozecchini | 0:9fca2b23d0ba | 314 | ticker1 = new Ticker(); |
marcozecchini | 0:9fca2b23d0ba | 315 | ticker2 = new Ticker(); |
marcozecchini | 0:9fca2b23d0ba | 316 | return utest::v1::greentea_case_setup_handler(source, index_of_case); |
marcozecchini | 0:9fca2b23d0ba | 317 | } |
marcozecchini | 0:9fca2b23d0ba | 318 | |
marcozecchini | 0:9fca2b23d0ba | 319 | utest::v1::status_t one_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed, const utest::v1::failure_t reason) |
marcozecchini | 0:9fca2b23d0ba | 320 | { |
marcozecchini | 0:9fca2b23d0ba | 321 | Ticker *temp1 = ticker1; |
marcozecchini | 0:9fca2b23d0ba | 322 | ticker1 = NULL; |
marcozecchini | 0:9fca2b23d0ba | 323 | delete temp1; |
marcozecchini | 0:9fca2b23d0ba | 324 | return utest::v1::greentea_case_teardown_handler(source, passed, failed, reason); |
marcozecchini | 0:9fca2b23d0ba | 325 | } |
marcozecchini | 0:9fca2b23d0ba | 326 | |
marcozecchini | 0:9fca2b23d0ba | 327 | utest::v1::status_t two_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed, const utest::v1::failure_t reason) |
marcozecchini | 0:9fca2b23d0ba | 328 | { |
marcozecchini | 0:9fca2b23d0ba | 329 | Ticker *temp1 = ticker1; |
marcozecchini | 0:9fca2b23d0ba | 330 | Ticker *temp2 = ticker2; |
marcozecchini | 0:9fca2b23d0ba | 331 | ticker1 = NULL; |
marcozecchini | 0:9fca2b23d0ba | 332 | ticker2 = NULL; |
marcozecchini | 0:9fca2b23d0ba | 333 | delete temp1; |
marcozecchini | 0:9fca2b23d0ba | 334 | delete temp2; |
marcozecchini | 0:9fca2b23d0ba | 335 | return utest::v1::greentea_case_teardown_handler(source, passed, failed, reason); |
marcozecchini | 0:9fca2b23d0ba | 336 | } |
marcozecchini | 0:9fca2b23d0ba | 337 | |
marcozecchini | 0:9fca2b23d0ba | 338 | |
marcozecchini | 0:9fca2b23d0ba | 339 | // Test cases |
marcozecchini | 0:9fca2b23d0ba | 340 | Case cases[] = { |
marcozecchini | 0:9fca2b23d0ba | 341 | Case("Test attach for 0.01s and time measure", test_attach_time<10000>), |
marcozecchini | 0:9fca2b23d0ba | 342 | Case("Test attach_us for 10ms and time measure", test_attach_us_time<10000>), |
marcozecchini | 0:9fca2b23d0ba | 343 | Case("Test attach for 0.1s and time measure", test_attach_time<100000>), |
marcozecchini | 0:9fca2b23d0ba | 344 | Case("Test attach_us for 100ms and time measure", test_attach_us_time<100000>), |
marcozecchini | 0:9fca2b23d0ba | 345 | Case("Test attach for 0.5s and time measure", test_attach_time<500000>), |
marcozecchini | 0:9fca2b23d0ba | 346 | Case("Test attach_us for 500ms and time measure", test_attach_us_time<500000>), |
marcozecchini | 0:9fca2b23d0ba | 347 | Case("Test detach", test_detach), |
marcozecchini | 0:9fca2b23d0ba | 348 | Case("Test multi call and time measure", test_multi_call_time), |
marcozecchini | 0:9fca2b23d0ba | 349 | Case("Test multi ticker", test_multi_ticker), |
marcozecchini | 0:9fca2b23d0ba | 350 | Case("Test timers: 1x ticker", one_ticker_case_setup_handler_t,test_case_1x_ticker, one_ticker_case_teardown_handler_t), |
marcozecchini | 0:9fca2b23d0ba | 351 | Case("Test timers: 2x callbacks", two_ticker_case_setup_handler_t,test_case_2x_callbacks, two_ticker_case_teardown_handler_t) |
marcozecchini | 0:9fca2b23d0ba | 352 | }; |
marcozecchini | 0:9fca2b23d0ba | 353 | |
marcozecchini | 0:9fca2b23d0ba | 354 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
marcozecchini | 0:9fca2b23d0ba | 355 | { |
marcozecchini | 0:9fca2b23d0ba | 356 | GREENTEA_SETUP(test_timeout, "timing_drift_auto"); |
marcozecchini | 0:9fca2b23d0ba | 357 | return utest::v1::greentea_test_setup_handler(number_of_cases); |
marcozecchini | 0:9fca2b23d0ba | 358 | } |
marcozecchini | 0:9fca2b23d0ba | 359 | |
marcozecchini | 0:9fca2b23d0ba | 360 | utest::v1::Specification specification(greentea_test_setup, cases, utest::v1::greentea_test_teardown_handler); |
marcozecchini | 0:9fca2b23d0ba | 361 | |
marcozecchini | 0:9fca2b23d0ba | 362 | int main() |
marcozecchini | 0:9fca2b23d0ba | 363 | { |
marcozecchini | 0:9fca2b23d0ba | 364 | utest::v1::Harness::run(specification); |
marcozecchini | 0:9fca2b23d0ba | 365 | } |