takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbed_drivers/timerevent/main.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2017 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | #if !DEVICE_USTICKER |
kadonotakashi | 0:8fdf9a60065b | 17 | #error [NOT_SUPPORTED] usticker not supported for this target. |
kadonotakashi | 0:8fdf9a60065b | 18 | #endif |
kadonotakashi | 0:8fdf9a60065b | 19 | |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "unity.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "utest.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "drivers/TimerEvent.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | #include "hal/ticker_api.h" |
kadonotakashi | 0:8fdf9a60065b | 26 | #include "rtos.h" |
kadonotakashi | 0:8fdf9a60065b | 27 | |
kadonotakashi | 0:8fdf9a60065b | 28 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 29 | |
kadonotakashi | 0:8fdf9a60065b | 30 | #if !DEVICE_USTICKER |
kadonotakashi | 0:8fdf9a60065b | 31 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 32 | #endif |
kadonotakashi | 0:8fdf9a60065b | 33 | |
kadonotakashi | 0:8fdf9a60065b | 34 | #define TEST_DELAY_US 50000ULL |
kadonotakashi | 0:8fdf9a60065b | 35 | #define DELTA 2 |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | class TestTimerEvent: public TimerEvent { |
kadonotakashi | 0:8fdf9a60065b | 38 | private: |
kadonotakashi | 0:8fdf9a60065b | 39 | Semaphore sem; |
kadonotakashi | 0:8fdf9a60065b | 40 | virtual void handler() |
kadonotakashi | 0:8fdf9a60065b | 41 | { |
kadonotakashi | 0:8fdf9a60065b | 42 | sem.release(); |
kadonotakashi | 0:8fdf9a60065b | 43 | } |
kadonotakashi | 0:8fdf9a60065b | 44 | |
kadonotakashi | 0:8fdf9a60065b | 45 | public: |
kadonotakashi | 0:8fdf9a60065b | 46 | TestTimerEvent() : |
kadonotakashi | 0:8fdf9a60065b | 47 | TimerEvent(), sem(0, 1) |
kadonotakashi | 0:8fdf9a60065b | 48 | { |
kadonotakashi | 0:8fdf9a60065b | 49 | |
kadonotakashi | 0:8fdf9a60065b | 50 | sleep_manager_lock_deep_sleep(); |
kadonotakashi | 0:8fdf9a60065b | 51 | } |
kadonotakashi | 0:8fdf9a60065b | 52 | |
kadonotakashi | 0:8fdf9a60065b | 53 | TestTimerEvent(const ticker_data_t *data) : |
kadonotakashi | 0:8fdf9a60065b | 54 | TimerEvent(data), sem(0, 1) |
kadonotakashi | 0:8fdf9a60065b | 55 | { |
kadonotakashi | 0:8fdf9a60065b | 56 | } |
kadonotakashi | 0:8fdf9a60065b | 57 | |
kadonotakashi | 0:8fdf9a60065b | 58 | virtual ~TestTimerEvent() |
kadonotakashi | 0:8fdf9a60065b | 59 | { |
kadonotakashi | 0:8fdf9a60065b | 60 | sleep_manager_unlock_deep_sleep(); |
kadonotakashi | 0:8fdf9a60065b | 61 | } |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | // Make these methods publicly accessible |
kadonotakashi | 0:8fdf9a60065b | 64 | using TimerEvent::insert; |
kadonotakashi | 0:8fdf9a60065b | 65 | using TimerEvent::insert_absolute; |
kadonotakashi | 0:8fdf9a60065b | 66 | using TimerEvent::remove; |
kadonotakashi | 0:8fdf9a60065b | 67 | |
kadonotakashi | 0:8fdf9a60065b | 68 | int32_t sem_wait(uint32_t millisec) |
kadonotakashi | 0:8fdf9a60065b | 69 | { |
kadonotakashi | 0:8fdf9a60065b | 70 | return sem.wait(millisec); |
kadonotakashi | 0:8fdf9a60065b | 71 | } |
kadonotakashi | 0:8fdf9a60065b | 72 | }; |
kadonotakashi | 0:8fdf9a60065b | 73 | |
kadonotakashi | 0:8fdf9a60065b | 74 | class TestTimerEventRelative: public TestTimerEvent { |
kadonotakashi | 0:8fdf9a60065b | 75 | public: |
kadonotakashi | 0:8fdf9a60065b | 76 | static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 0; |
kadonotakashi | 0:8fdf9a60065b | 77 | TestTimerEventRelative() : |
kadonotakashi | 0:8fdf9a60065b | 78 | TestTimerEvent() |
kadonotakashi | 0:8fdf9a60065b | 79 | { |
kadonotakashi | 0:8fdf9a60065b | 80 | } |
kadonotakashi | 0:8fdf9a60065b | 81 | |
kadonotakashi | 0:8fdf9a60065b | 82 | TestTimerEventRelative(const ticker_data_t *data) : |
kadonotakashi | 0:8fdf9a60065b | 83 | TestTimerEvent(data) |
kadonotakashi | 0:8fdf9a60065b | 84 | { |
kadonotakashi | 0:8fdf9a60065b | 85 | } |
kadonotakashi | 0:8fdf9a60065b | 86 | |
kadonotakashi | 0:8fdf9a60065b | 87 | // Set relative timestamp of internal event to present_time + ts |
kadonotakashi | 0:8fdf9a60065b | 88 | void set_future_timestamp(timestamp_t ts) |
kadonotakashi | 0:8fdf9a60065b | 89 | { |
kadonotakashi | 0:8fdf9a60065b | 90 | insert(::ticker_read(_ticker_data) + ts); |
kadonotakashi | 0:8fdf9a60065b | 91 | } |
kadonotakashi | 0:8fdf9a60065b | 92 | |
kadonotakashi | 0:8fdf9a60065b | 93 | void set_past_timestamp(void) |
kadonotakashi | 0:8fdf9a60065b | 94 | { |
kadonotakashi | 0:8fdf9a60065b | 95 | insert(::ticker_read(_ticker_data) - 1UL); |
kadonotakashi | 0:8fdf9a60065b | 96 | } |
kadonotakashi | 0:8fdf9a60065b | 97 | }; |
kadonotakashi | 0:8fdf9a60065b | 98 | |
kadonotakashi | 0:8fdf9a60065b | 99 | class TestTimerEventAbsolute: public TestTimerEvent { |
kadonotakashi | 0:8fdf9a60065b | 100 | public: |
kadonotakashi | 0:8fdf9a60065b | 101 | static const int32_t SEM_SLOTS_AFTER_PAST_TS_INSERTED = 1; |
kadonotakashi | 0:8fdf9a60065b | 102 | TestTimerEventAbsolute() : |
kadonotakashi | 0:8fdf9a60065b | 103 | TestTimerEvent() |
kadonotakashi | 0:8fdf9a60065b | 104 | { |
kadonotakashi | 0:8fdf9a60065b | 105 | } |
kadonotakashi | 0:8fdf9a60065b | 106 | |
kadonotakashi | 0:8fdf9a60065b | 107 | TestTimerEventAbsolute(const ticker_data_t *data) : |
kadonotakashi | 0:8fdf9a60065b | 108 | TestTimerEvent(data) |
kadonotakashi | 0:8fdf9a60065b | 109 | { |
kadonotakashi | 0:8fdf9a60065b | 110 | } |
kadonotakashi | 0:8fdf9a60065b | 111 | |
kadonotakashi | 0:8fdf9a60065b | 112 | // Set absolute timestamp of internal event to present_time + ts |
kadonotakashi | 0:8fdf9a60065b | 113 | void set_future_timestamp(us_timestamp_t ts) |
kadonotakashi | 0:8fdf9a60065b | 114 | { |
kadonotakashi | 0:8fdf9a60065b | 115 | insert_absolute(::ticker_read_us(_ticker_data) + ts); |
kadonotakashi | 0:8fdf9a60065b | 116 | } |
kadonotakashi | 0:8fdf9a60065b | 117 | |
kadonotakashi | 0:8fdf9a60065b | 118 | void set_past_timestamp(void) |
kadonotakashi | 0:8fdf9a60065b | 119 | { |
kadonotakashi | 0:8fdf9a60065b | 120 | insert_absolute(::ticker_read_us(_ticker_data) - 1ULL); |
kadonotakashi | 0:8fdf9a60065b | 121 | } |
kadonotakashi | 0:8fdf9a60065b | 122 | }; |
kadonotakashi | 0:8fdf9a60065b | 123 | |
kadonotakashi | 0:8fdf9a60065b | 124 | /** Template for tests: insert, insert_absolute |
kadonotakashi | 0:8fdf9a60065b | 125 | * |
kadonotakashi | 0:8fdf9a60065b | 126 | * Test insert |
kadonotakashi | 0:8fdf9a60065b | 127 | * Given an instance of @a TimerEvent subclass |
kadonotakashi | 0:8fdf9a60065b | 128 | * When a tiestamp is set with @a insert() |
kadonotakashi | 0:8fdf9a60065b | 129 | * and given time elapses |
kadonotakashi | 0:8fdf9a60065b | 130 | * Then an event handler is called |
kadonotakashi | 0:8fdf9a60065b | 131 | * |
kadonotakashi | 0:8fdf9a60065b | 132 | * Test insert_absolute |
kadonotakashi | 0:8fdf9a60065b | 133 | * Given an instance of @a TimerEvent subclass |
kadonotakashi | 0:8fdf9a60065b | 134 | * When a tiestamp is set with @a insert_absolute() |
kadonotakashi | 0:8fdf9a60065b | 135 | * and given time elapses |
kadonotakashi | 0:8fdf9a60065b | 136 | * Then an event handler is called |
kadonotakashi | 0:8fdf9a60065b | 137 | */ |
kadonotakashi | 0:8fdf9a60065b | 138 | template<typename T> |
kadonotakashi | 0:8fdf9a60065b | 139 | void test_insert(void) |
kadonotakashi | 0:8fdf9a60065b | 140 | { |
kadonotakashi | 0:8fdf9a60065b | 141 | T tte; |
kadonotakashi | 0:8fdf9a60065b | 142 | |
kadonotakashi | 0:8fdf9a60065b | 143 | tte.set_future_timestamp(TEST_DELAY_US); |
kadonotakashi | 0:8fdf9a60065b | 144 | int32_t sem_slots = tte.sem_wait(0); |
kadonotakashi | 0:8fdf9a60065b | 145 | TEST_ASSERT_EQUAL(0, sem_slots); |
kadonotakashi | 0:8fdf9a60065b | 146 | |
kadonotakashi | 0:8fdf9a60065b | 147 | sem_slots = tte.sem_wait(TEST_DELAY_US / 1000 + DELTA); |
kadonotakashi | 0:8fdf9a60065b | 148 | TEST_ASSERT_EQUAL(1, sem_slots); |
kadonotakashi | 0:8fdf9a60065b | 149 | |
kadonotakashi | 0:8fdf9a60065b | 150 | tte.remove(); |
kadonotakashi | 0:8fdf9a60065b | 151 | } |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | /** Template for tests: remove |
kadonotakashi | 0:8fdf9a60065b | 154 | * |
kadonotakashi | 0:8fdf9a60065b | 155 | * Test remove after insert |
kadonotakashi | 0:8fdf9a60065b | 156 | * Given an instance of @a TimerEvent subclass |
kadonotakashi | 0:8fdf9a60065b | 157 | * When a tiestamp is set with @a insert() |
kadonotakashi | 0:8fdf9a60065b | 158 | * and timestamp is removed before being reached |
kadonotakashi | 0:8fdf9a60065b | 159 | * Then the event handler is never called |
kadonotakashi | 0:8fdf9a60065b | 160 | * |
kadonotakashi | 0:8fdf9a60065b | 161 | * Test remove after insert_absolute |
kadonotakashi | 0:8fdf9a60065b | 162 | * Given an instance of @a TimerEvent subclass |
kadonotakashi | 0:8fdf9a60065b | 163 | * When a tiestamp is set with @a insert_absolute() |
kadonotakashi | 0:8fdf9a60065b | 164 | * and timestamp is removed before being reached |
kadonotakashi | 0:8fdf9a60065b | 165 | * Then the event handler is never called |
kadonotakashi | 0:8fdf9a60065b | 166 | */ |
kadonotakashi | 0:8fdf9a60065b | 167 | template<typename T> |
kadonotakashi | 0:8fdf9a60065b | 168 | void test_remove(void) |
kadonotakashi | 0:8fdf9a60065b | 169 | { |
kadonotakashi | 0:8fdf9a60065b | 170 | T tte; |
kadonotakashi | 0:8fdf9a60065b | 171 | |
kadonotakashi | 0:8fdf9a60065b | 172 | tte.set_future_timestamp(TEST_DELAY_US * 2); |
kadonotakashi | 0:8fdf9a60065b | 173 | int32_t sem_slots = tte.sem_wait(TEST_DELAY_US / 1000); |
kadonotakashi | 0:8fdf9a60065b | 174 | TEST_ASSERT_EQUAL(0, sem_slots); |
kadonotakashi | 0:8fdf9a60065b | 175 | tte.remove(); |
kadonotakashi | 0:8fdf9a60065b | 176 | |
kadonotakashi | 0:8fdf9a60065b | 177 | sem_slots = tte.sem_wait(TEST_DELAY_US * 2 / 1000 + DELTA); |
kadonotakashi | 0:8fdf9a60065b | 178 | TEST_ASSERT_EQUAL(0, sem_slots); |
kadonotakashi | 0:8fdf9a60065b | 179 | } |
kadonotakashi | 0:8fdf9a60065b | 180 | |
kadonotakashi | 0:8fdf9a60065b | 181 | /** Test insert_absolute zero |
kadonotakashi | 0:8fdf9a60065b | 182 | * Given an instance of @a TimerEvent subclass |
kadonotakashi | 0:8fdf9a60065b | 183 | * When a timestamp of 0 us is set with @a insert_absolute() |
kadonotakashi | 0:8fdf9a60065b | 184 | * Then an event handler is called instantly |
kadonotakashi | 0:8fdf9a60065b | 185 | */ |
kadonotakashi | 0:8fdf9a60065b | 186 | void test_insert_zero(void) |
kadonotakashi | 0:8fdf9a60065b | 187 | { |
kadonotakashi | 0:8fdf9a60065b | 188 | TestTimerEvent tte; |
kadonotakashi | 0:8fdf9a60065b | 189 | |
kadonotakashi | 0:8fdf9a60065b | 190 | tte.insert_absolute(0ULL); |
kadonotakashi | 0:8fdf9a60065b | 191 | int32_t sem_slots = tte.sem_wait(0); |
kadonotakashi | 0:8fdf9a60065b | 192 | TEST_ASSERT_EQUAL(1, sem_slots); |
kadonotakashi | 0:8fdf9a60065b | 193 | |
kadonotakashi | 0:8fdf9a60065b | 194 | tte.remove(); |
kadonotakashi | 0:8fdf9a60065b | 195 | } |
kadonotakashi | 0:8fdf9a60065b | 196 | |
kadonotakashi | 0:8fdf9a60065b | 197 | /** Template for tests: insert, insert_absolute past timestamp |
kadonotakashi | 0:8fdf9a60065b | 198 | * |
kadonotakashi | 0:8fdf9a60065b | 199 | * Test insert timestamp from the past |
kadonotakashi | 0:8fdf9a60065b | 200 | * Given an instance of @a TimerEvent subclass |
kadonotakashi | 0:8fdf9a60065b | 201 | * When a timestamp of X us is set with @a insert() |
kadonotakashi | 0:8fdf9a60065b | 202 | * and X is less than present_time |
kadonotakashi | 0:8fdf9a60065b | 203 | * Then an event handler is **not** called instantly |
kadonotakashi | 0:8fdf9a60065b | 204 | * (the event is scheduled after the ticker's overflow) |
kadonotakashi | 0:8fdf9a60065b | 205 | * |
kadonotakashi | 0:8fdf9a60065b | 206 | * Test insert_absolute timestamp from the past |
kadonotakashi | 0:8fdf9a60065b | 207 | * Given an instance of @a TimerEvent subclass |
kadonotakashi | 0:8fdf9a60065b | 208 | * When a timestamp of X us is set with @a insert_absolute() |
kadonotakashi | 0:8fdf9a60065b | 209 | * and X is less than present_time |
kadonotakashi | 0:8fdf9a60065b | 210 | * Then an event handler is called instantly |
kadonotakashi | 0:8fdf9a60065b | 211 | */ |
kadonotakashi | 0:8fdf9a60065b | 212 | template<typename T> |
kadonotakashi | 0:8fdf9a60065b | 213 | void test_insert_past(void) |
kadonotakashi | 0:8fdf9a60065b | 214 | { |
kadonotakashi | 0:8fdf9a60065b | 215 | T tte; |
kadonotakashi | 0:8fdf9a60065b | 216 | |
kadonotakashi | 0:8fdf9a60065b | 217 | tte.set_past_timestamp(); |
kadonotakashi | 0:8fdf9a60065b | 218 | int32_t sem_slots = tte.sem_wait(0); |
kadonotakashi | 0:8fdf9a60065b | 219 | TEST_ASSERT_EQUAL(tte.SEM_SLOTS_AFTER_PAST_TS_INSERTED, sem_slots); |
kadonotakashi | 0:8fdf9a60065b | 220 | |
kadonotakashi | 0:8fdf9a60065b | 221 | tte.remove(); |
kadonotakashi | 0:8fdf9a60065b | 222 | } |
kadonotakashi | 0:8fdf9a60065b | 223 | |
kadonotakashi | 0:8fdf9a60065b | 224 | utest::v1::status_t test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 225 | { |
kadonotakashi | 0:8fdf9a60065b | 226 | GREENTEA_SETUP(5, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 227 | return verbose_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 228 | } |
kadonotakashi | 0:8fdf9a60065b | 229 | |
kadonotakashi | 0:8fdf9a60065b | 230 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 231 | Case("Test insert", test_insert<TestTimerEventRelative>), |
kadonotakashi | 0:8fdf9a60065b | 232 | Case("Test insert_absolute", test_insert<TestTimerEventAbsolute>), |
kadonotakashi | 0:8fdf9a60065b | 233 | |
kadonotakashi | 0:8fdf9a60065b | 234 | Case("Test remove after insert", test_remove<TestTimerEventRelative>), |
kadonotakashi | 0:8fdf9a60065b | 235 | Case("Test remove after insert_absolute", test_remove<TestTimerEventAbsolute>), |
kadonotakashi | 0:8fdf9a60065b | 236 | |
kadonotakashi | 0:8fdf9a60065b | 237 | Case("Test insert_absolute zero", test_insert_zero), |
kadonotakashi | 0:8fdf9a60065b | 238 | |
kadonotakashi | 0:8fdf9a60065b | 239 | Case("Test insert timestamp from the past", test_insert_past<TestTimerEventRelative>), |
kadonotakashi | 0:8fdf9a60065b | 240 | Case("Test insert_absolute timestamp from the past", test_insert_past<TestTimerEventAbsolute>), |
kadonotakashi | 0:8fdf9a60065b | 241 | }; |
kadonotakashi | 0:8fdf9a60065b | 242 | |
kadonotakashi | 0:8fdf9a60065b | 243 | Specification specification(test_setup, cases); |
kadonotakashi | 0:8fdf9a60065b | 244 | |
kadonotakashi | 0:8fdf9a60065b | 245 | int main() |
kadonotakashi | 0:8fdf9a60065b | 246 | { |
kadonotakashi | 0:8fdf9a60065b | 247 | return !Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 248 | } |