takashi kadono
/
Nucleo446_SSD1331
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbedmicro-rtos-mbed/rtostimer/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 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "unity.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "utest.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "rtos.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | |
kadonotakashi | 0:8fdf9a60065b | 22 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 23 | |
kadonotakashi | 0:8fdf9a60065b | 24 | #define DELAY_MS 50 |
kadonotakashi | 0:8fdf9a60065b | 25 | #define DELTA_MS 5 |
kadonotakashi | 0:8fdf9a60065b | 26 | #define RESTART_DELAY_MS 10 |
kadonotakashi | 0:8fdf9a60065b | 27 | #define DELAY2_MS 30 |
kadonotakashi | 0:8fdf9a60065b | 28 | |
kadonotakashi | 0:8fdf9a60065b | 29 | #if RESTART_DELAY_MS >= DELAY_MS |
kadonotakashi | 0:8fdf9a60065b | 30 | #error invalid RESTART_DELAY_MS value |
kadonotakashi | 0:8fdf9a60065b | 31 | #endif |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | #if !DEVICE_USTICKER |
kadonotakashi | 0:8fdf9a60065b | 34 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 35 | #endif |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | class Stopwatch: public Timer { |
kadonotakashi | 0:8fdf9a60065b | 38 | private: |
kadonotakashi | 0:8fdf9a60065b | 39 | Semaphore _sem; |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | public: |
kadonotakashi | 0:8fdf9a60065b | 42 | Stopwatch() : |
kadonotakashi | 0:8fdf9a60065b | 43 | Timer(), _sem(1) |
kadonotakashi | 0:8fdf9a60065b | 44 | { |
kadonotakashi | 0:8fdf9a60065b | 45 | } |
kadonotakashi | 0:8fdf9a60065b | 46 | |
kadonotakashi | 0:8fdf9a60065b | 47 | ~Stopwatch() |
kadonotakashi | 0:8fdf9a60065b | 48 | { |
kadonotakashi | 0:8fdf9a60065b | 49 | } |
kadonotakashi | 0:8fdf9a60065b | 50 | |
kadonotakashi | 0:8fdf9a60065b | 51 | void start(void) |
kadonotakashi | 0:8fdf9a60065b | 52 | { |
kadonotakashi | 0:8fdf9a60065b | 53 | _sem.wait(0); |
kadonotakashi | 0:8fdf9a60065b | 54 | Timer::start(); |
kadonotakashi | 0:8fdf9a60065b | 55 | } |
kadonotakashi | 0:8fdf9a60065b | 56 | |
kadonotakashi | 0:8fdf9a60065b | 57 | void stop(void) |
kadonotakashi | 0:8fdf9a60065b | 58 | { |
kadonotakashi | 0:8fdf9a60065b | 59 | Timer::stop(); |
kadonotakashi | 0:8fdf9a60065b | 60 | _sem.release(); |
kadonotakashi | 0:8fdf9a60065b | 61 | } |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | int32_t wait_until_stopped(uint32_t millisec = osWaitForever) |
kadonotakashi | 0:8fdf9a60065b | 64 | { |
kadonotakashi | 0:8fdf9a60065b | 65 | core_util_critical_section_enter(); |
kadonotakashi | 0:8fdf9a60065b | 66 | int running = _running; |
kadonotakashi | 0:8fdf9a60065b | 67 | core_util_critical_section_exit(); |
kadonotakashi | 0:8fdf9a60065b | 68 | if (!running) { |
kadonotakashi | 0:8fdf9a60065b | 69 | return 1; |
kadonotakashi | 0:8fdf9a60065b | 70 | } |
kadonotakashi | 0:8fdf9a60065b | 71 | return _sem.wait(millisec); |
kadonotakashi | 0:8fdf9a60065b | 72 | } |
kadonotakashi | 0:8fdf9a60065b | 73 | }; |
kadonotakashi | 0:8fdf9a60065b | 74 | |
kadonotakashi | 0:8fdf9a60065b | 75 | void sem_callback(Semaphore *sem) |
kadonotakashi | 0:8fdf9a60065b | 76 | { |
kadonotakashi | 0:8fdf9a60065b | 77 | sem->release(); |
kadonotakashi | 0:8fdf9a60065b | 78 | } |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | /* In order to successfully run this test suite when compiled with --profile=debug |
kadonotakashi | 0:8fdf9a60065b | 81 | * error() has to be redefined as noop. |
kadonotakashi | 0:8fdf9a60065b | 82 | * |
kadonotakashi | 0:8fdf9a60065b | 83 | * RtosTimer calls RTX API which uses Event Recorder functionality. When compiled |
kadonotakashi | 0:8fdf9a60065b | 84 | * with MBED_TRAP_ERRORS_ENABLED=1 (set in debug profile) EvrRtxTimerError() calls error() |
kadonotakashi | 0:8fdf9a60065b | 85 | * which aborts test program. |
kadonotakashi | 0:8fdf9a60065b | 86 | */ |
kadonotakashi | 0:8fdf9a60065b | 87 | #if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED |
kadonotakashi | 0:8fdf9a60065b | 88 | void error(const char *format, ...) |
kadonotakashi | 0:8fdf9a60065b | 89 | { |
kadonotakashi | 0:8fdf9a60065b | 90 | (void) format; |
kadonotakashi | 0:8fdf9a60065b | 91 | } |
kadonotakashi | 0:8fdf9a60065b | 92 | |
kadonotakashi | 0:8fdf9a60065b | 93 | mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number) |
kadonotakashi | 0:8fdf9a60065b | 94 | { |
kadonotakashi | 0:8fdf9a60065b | 95 | return MBED_SUCCESS; |
kadonotakashi | 0:8fdf9a60065b | 96 | } |
kadonotakashi | 0:8fdf9a60065b | 97 | #endif |
kadonotakashi | 0:8fdf9a60065b | 98 | |
kadonotakashi | 0:8fdf9a60065b | 99 | /** Test one-shot not restarted when elapsed |
kadonotakashi | 0:8fdf9a60065b | 100 | * |
kadonotakashi | 0:8fdf9a60065b | 101 | * Given a one-shot RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 102 | * When the timer is started |
kadonotakashi | 0:8fdf9a60065b | 103 | * and given time elapses |
kadonotakashi | 0:8fdf9a60065b | 104 | * Then timer stops |
kadonotakashi | 0:8fdf9a60065b | 105 | * and elapsed time matches given delay |
kadonotakashi | 0:8fdf9a60065b | 106 | * and timer stays stopped |
kadonotakashi | 0:8fdf9a60065b | 107 | */ |
kadonotakashi | 0:8fdf9a60065b | 108 | void test_oneshot_not_restarted() |
kadonotakashi | 0:8fdf9a60065b | 109 | { |
kadonotakashi | 0:8fdf9a60065b | 110 | Stopwatch stopwatch; |
kadonotakashi | 0:8fdf9a60065b | 111 | RtosTimer rtostimer(mbed::callback(&stopwatch, &Stopwatch::stop), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 112 | |
kadonotakashi | 0:8fdf9a60065b | 113 | stopwatch.start(); |
kadonotakashi | 0:8fdf9a60065b | 114 | osStatus status = rtostimer.start(DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 115 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 116 | |
kadonotakashi | 0:8fdf9a60065b | 117 | int32_t slots = stopwatch.wait_until_stopped(); |
kadonotakashi | 0:8fdf9a60065b | 118 | TEST_ASSERT_EQUAL(1, slots); |
kadonotakashi | 0:8fdf9a60065b | 119 | TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY_MS, stopwatch.read_ms()); |
kadonotakashi | 0:8fdf9a60065b | 120 | stopwatch.start(); |
kadonotakashi | 0:8fdf9a60065b | 121 | |
kadonotakashi | 0:8fdf9a60065b | 122 | slots = stopwatch.wait_until_stopped(DELAY_MS + DELTA_MS); |
kadonotakashi | 0:8fdf9a60065b | 123 | TEST_ASSERT_EQUAL(0, slots); |
kadonotakashi | 0:8fdf9a60065b | 124 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 125 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 126 | } |
kadonotakashi | 0:8fdf9a60065b | 127 | |
kadonotakashi | 0:8fdf9a60065b | 128 | /** Test periodic repeats continuously |
kadonotakashi | 0:8fdf9a60065b | 129 | * |
kadonotakashi | 0:8fdf9a60065b | 130 | * Given a periodic RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 131 | * When timer is started |
kadonotakashi | 0:8fdf9a60065b | 132 | * and given time elapses |
kadonotakashi | 0:8fdf9a60065b | 133 | * Then timer repeats its operation |
kadonotakashi | 0:8fdf9a60065b | 134 | * When timer is stopped |
kadonotakashi | 0:8fdf9a60065b | 135 | * Then timer stops operation |
kadonotakashi | 0:8fdf9a60065b | 136 | */ |
kadonotakashi | 0:8fdf9a60065b | 137 | void test_periodic_repeats() |
kadonotakashi | 0:8fdf9a60065b | 138 | { |
kadonotakashi | 0:8fdf9a60065b | 139 | Stopwatch stopwatch; |
kadonotakashi | 0:8fdf9a60065b | 140 | RtosTimer rtostimer(mbed::callback(&stopwatch, &Stopwatch::stop), osTimerPeriodic); |
kadonotakashi | 0:8fdf9a60065b | 141 | |
kadonotakashi | 0:8fdf9a60065b | 142 | stopwatch.start(); |
kadonotakashi | 0:8fdf9a60065b | 143 | osStatus status = rtostimer.start(DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 144 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 145 | |
kadonotakashi | 0:8fdf9a60065b | 146 | int32_t slots = stopwatch.wait_until_stopped(); |
kadonotakashi | 0:8fdf9a60065b | 147 | int t1 = stopwatch.read_ms(); |
kadonotakashi | 0:8fdf9a60065b | 148 | stopwatch.reset(); |
kadonotakashi | 0:8fdf9a60065b | 149 | stopwatch.start(); |
kadonotakashi | 0:8fdf9a60065b | 150 | TEST_ASSERT_EQUAL(1, slots); |
kadonotakashi | 0:8fdf9a60065b | 151 | TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY_MS, t1); |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | slots = stopwatch.wait_until_stopped(); |
kadonotakashi | 0:8fdf9a60065b | 154 | TEST_ASSERT_EQUAL(1, slots); |
kadonotakashi | 0:8fdf9a60065b | 155 | TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY_MS, stopwatch.read_ms()); |
kadonotakashi | 0:8fdf9a60065b | 156 | stopwatch.start(); |
kadonotakashi | 0:8fdf9a60065b | 157 | |
kadonotakashi | 0:8fdf9a60065b | 158 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 159 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 160 | |
kadonotakashi | 0:8fdf9a60065b | 161 | slots = stopwatch.wait_until_stopped(DELAY_MS + DELTA_MS); |
kadonotakashi | 0:8fdf9a60065b | 162 | TEST_ASSERT_EQUAL(0, slots); |
kadonotakashi | 0:8fdf9a60065b | 163 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 164 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 165 | } |
kadonotakashi | 0:8fdf9a60065b | 166 | |
kadonotakashi | 0:8fdf9a60065b | 167 | /** Test timer can be started again |
kadonotakashi | 0:8fdf9a60065b | 168 | * |
kadonotakashi | 0:8fdf9a60065b | 169 | * Given a one-shot Rtosimer |
kadonotakashi | 0:8fdf9a60065b | 170 | * When the timer is started |
kadonotakashi | 0:8fdf9a60065b | 171 | * and given time elapses |
kadonotakashi | 0:8fdf9a60065b | 172 | * Then timer stops |
kadonotakashi | 0:8fdf9a60065b | 173 | * When the timer is started again |
kadonotakashi | 0:8fdf9a60065b | 174 | * and given time elapses |
kadonotakashi | 0:8fdf9a60065b | 175 | * Then timer stops again |
kadonotakashi | 0:8fdf9a60065b | 176 | */ |
kadonotakashi | 0:8fdf9a60065b | 177 | void test_start_again() |
kadonotakashi | 0:8fdf9a60065b | 178 | { |
kadonotakashi | 0:8fdf9a60065b | 179 | Semaphore sem(0, 1); |
kadonotakashi | 0:8fdf9a60065b | 180 | RtosTimer rtostimer(mbed::callback(sem_callback, &sem), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 181 | |
kadonotakashi | 0:8fdf9a60065b | 182 | osStatus status = rtostimer.start(DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 183 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 184 | |
kadonotakashi | 0:8fdf9a60065b | 185 | int32_t slots = sem.wait(DELAY_MS + DELTA_MS); |
kadonotakashi | 0:8fdf9a60065b | 186 | TEST_ASSERT_EQUAL(1, slots); |
kadonotakashi | 0:8fdf9a60065b | 187 | |
kadonotakashi | 0:8fdf9a60065b | 188 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 189 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 190 | |
kadonotakashi | 0:8fdf9a60065b | 191 | status = rtostimer.start(DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 192 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 193 | |
kadonotakashi | 0:8fdf9a60065b | 194 | slots = sem.wait(DELAY_MS + DELTA_MS); |
kadonotakashi | 0:8fdf9a60065b | 195 | TEST_ASSERT_EQUAL(1, slots); |
kadonotakashi | 0:8fdf9a60065b | 196 | |
kadonotakashi | 0:8fdf9a60065b | 197 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 198 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 199 | } |
kadonotakashi | 0:8fdf9a60065b | 200 | |
kadonotakashi | 0:8fdf9a60065b | 201 | /** Test timer restart updates delay |
kadonotakashi | 0:8fdf9a60065b | 202 | * |
kadonotakashi | 0:8fdf9a60065b | 203 | * Given a one-shot RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 204 | * When the timer is started |
kadonotakashi | 0:8fdf9a60065b | 205 | * and @a start is called again with a different delay before given time elapses |
kadonotakashi | 0:8fdf9a60065b | 206 | * and updated delay elapses |
kadonotakashi | 0:8fdf9a60065b | 207 | * Then timer stops |
kadonotakashi | 0:8fdf9a60065b | 208 | * and time elapsed since the second @a start call matches updated delay |
kadonotakashi | 0:8fdf9a60065b | 209 | */ |
kadonotakashi | 0:8fdf9a60065b | 210 | void test_restart_updates_delay() |
kadonotakashi | 0:8fdf9a60065b | 211 | { |
kadonotakashi | 0:8fdf9a60065b | 212 | Stopwatch stopwatch; |
kadonotakashi | 0:8fdf9a60065b | 213 | RtosTimer rtostimer(mbed::callback(&stopwatch, &Stopwatch::stop), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 214 | |
kadonotakashi | 0:8fdf9a60065b | 215 | stopwatch.start(); |
kadonotakashi | 0:8fdf9a60065b | 216 | osStatus status = rtostimer.start(DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 217 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 218 | |
kadonotakashi | 0:8fdf9a60065b | 219 | int32_t slots = stopwatch.wait_until_stopped(RESTART_DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 220 | TEST_ASSERT_EQUAL(0, slots); |
kadonotakashi | 0:8fdf9a60065b | 221 | |
kadonotakashi | 0:8fdf9a60065b | 222 | stopwatch.reset(); |
kadonotakashi | 0:8fdf9a60065b | 223 | stopwatch.start(); |
kadonotakashi | 0:8fdf9a60065b | 224 | status = rtostimer.start(DELAY2_MS); |
kadonotakashi | 0:8fdf9a60065b | 225 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 226 | |
kadonotakashi | 0:8fdf9a60065b | 227 | slots = stopwatch.wait_until_stopped(); |
kadonotakashi | 0:8fdf9a60065b | 228 | TEST_ASSERT_EQUAL(1, slots); |
kadonotakashi | 0:8fdf9a60065b | 229 | TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY2_MS, stopwatch.read_ms()); |
kadonotakashi | 0:8fdf9a60065b | 230 | |
kadonotakashi | 0:8fdf9a60065b | 231 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 232 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 233 | } |
kadonotakashi | 0:8fdf9a60065b | 234 | |
kadonotakashi | 0:8fdf9a60065b | 235 | /** Test timer is created in stopped state |
kadonotakashi | 0:8fdf9a60065b | 236 | * |
kadonotakashi | 0:8fdf9a60065b | 237 | * Given a one-shot RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 238 | * When the timer has not been started |
kadonotakashi | 0:8fdf9a60065b | 239 | * Then the timer is stopped |
kadonotakashi | 0:8fdf9a60065b | 240 | */ |
kadonotakashi | 0:8fdf9a60065b | 241 | void test_created_stopped() |
kadonotakashi | 0:8fdf9a60065b | 242 | { |
kadonotakashi | 0:8fdf9a60065b | 243 | RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 244 | osStatus status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 245 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 246 | } |
kadonotakashi | 0:8fdf9a60065b | 247 | |
kadonotakashi | 0:8fdf9a60065b | 248 | /** Test one-shot can be stopped |
kadonotakashi | 0:8fdf9a60065b | 249 | * |
kadonotakashi | 0:8fdf9a60065b | 250 | * Given a one-shot RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 251 | * When the timer is started |
kadonotakashi | 0:8fdf9a60065b | 252 | * and timer is stopped while still running |
kadonotakashi | 0:8fdf9a60065b | 253 | * Then timer stops operation |
kadonotakashi | 0:8fdf9a60065b | 254 | */ |
kadonotakashi | 0:8fdf9a60065b | 255 | void test_stop() |
kadonotakashi | 0:8fdf9a60065b | 256 | { |
kadonotakashi | 0:8fdf9a60065b | 257 | Semaphore sem(0, 1); |
kadonotakashi | 0:8fdf9a60065b | 258 | RtosTimer rtostimer(mbed::callback(sem_callback, &sem), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 259 | |
kadonotakashi | 0:8fdf9a60065b | 260 | osStatus status = rtostimer.start(DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 261 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 262 | |
kadonotakashi | 0:8fdf9a60065b | 263 | int32_t slots = sem.wait(RESTART_DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 264 | TEST_ASSERT_EQUAL(0, slots); |
kadonotakashi | 0:8fdf9a60065b | 265 | |
kadonotakashi | 0:8fdf9a60065b | 266 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 267 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 268 | |
kadonotakashi | 0:8fdf9a60065b | 269 | slots = sem.wait(DELAY_MS + DELTA_MS); |
kadonotakashi | 0:8fdf9a60065b | 270 | TEST_ASSERT_EQUAL(0, slots); |
kadonotakashi | 0:8fdf9a60065b | 271 | |
kadonotakashi | 0:8fdf9a60065b | 272 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 273 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 274 | } |
kadonotakashi | 0:8fdf9a60065b | 275 | |
kadonotakashi | 0:8fdf9a60065b | 276 | /** Test timer started with infinite delay |
kadonotakashi | 0:8fdf9a60065b | 277 | * |
kadonotakashi | 0:8fdf9a60065b | 278 | * Given a one-shot RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 279 | * When the timer is started with @a osWaitForever delay |
kadonotakashi | 0:8fdf9a60065b | 280 | * Then @a start return status is @a osOK |
kadonotakashi | 0:8fdf9a60065b | 281 | */ |
kadonotakashi | 0:8fdf9a60065b | 282 | void test_wait_forever() |
kadonotakashi | 0:8fdf9a60065b | 283 | { |
kadonotakashi | 0:8fdf9a60065b | 284 | RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 285 | |
kadonotakashi | 0:8fdf9a60065b | 286 | osStatus status = rtostimer.start(osWaitForever); |
kadonotakashi | 0:8fdf9a60065b | 287 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 288 | |
kadonotakashi | 0:8fdf9a60065b | 289 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 290 | TEST_ASSERT_EQUAL(osOK, status); |
kadonotakashi | 0:8fdf9a60065b | 291 | } |
kadonotakashi | 0:8fdf9a60065b | 292 | |
kadonotakashi | 0:8fdf9a60065b | 293 | /** Test timer started with zero delay |
kadonotakashi | 0:8fdf9a60065b | 294 | * |
kadonotakashi | 0:8fdf9a60065b | 295 | * Given a one-shot RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 296 | * When the timer is started with 0 delay |
kadonotakashi | 0:8fdf9a60065b | 297 | * Then @a start return status is @a osErrorParameter |
kadonotakashi | 0:8fdf9a60065b | 298 | */ |
kadonotakashi | 0:8fdf9a60065b | 299 | void test_no_wait() |
kadonotakashi | 0:8fdf9a60065b | 300 | { |
kadonotakashi | 0:8fdf9a60065b | 301 | RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 302 | |
kadonotakashi | 0:8fdf9a60065b | 303 | osStatus status = rtostimer.start(0); |
kadonotakashi | 0:8fdf9a60065b | 304 | TEST_ASSERT_EQUAL(osErrorParameter, status); |
kadonotakashi | 0:8fdf9a60065b | 305 | |
kadonotakashi | 0:8fdf9a60065b | 306 | status = rtostimer.stop(); |
kadonotakashi | 0:8fdf9a60065b | 307 | TEST_ASSERT_EQUAL(osErrorResource, status); |
kadonotakashi | 0:8fdf9a60065b | 308 | } |
kadonotakashi | 0:8fdf9a60065b | 309 | |
kadonotakashi | 0:8fdf9a60065b | 310 | void rtostimer_isr_call(RtosTimer *rtostimer) |
kadonotakashi | 0:8fdf9a60065b | 311 | { |
kadonotakashi | 0:8fdf9a60065b | 312 | osStatus status = rtostimer->start(DELAY_MS); |
kadonotakashi | 0:8fdf9a60065b | 313 | TEST_ASSERT_EQUAL(osErrorISR, status); |
kadonotakashi | 0:8fdf9a60065b | 314 | |
kadonotakashi | 0:8fdf9a60065b | 315 | status = rtostimer->stop(); |
kadonotakashi | 0:8fdf9a60065b | 316 | TEST_ASSERT_EQUAL(osErrorISR, status); |
kadonotakashi | 0:8fdf9a60065b | 317 | } |
kadonotakashi | 0:8fdf9a60065b | 318 | |
kadonotakashi | 0:8fdf9a60065b | 319 | /** Test timer method calls from an ISR fail |
kadonotakashi | 0:8fdf9a60065b | 320 | * |
kadonotakashi | 0:8fdf9a60065b | 321 | * Given a one-shot RtosTimer |
kadonotakashi | 0:8fdf9a60065b | 322 | * When a timer method is called from an ISR |
kadonotakashi | 0:8fdf9a60065b | 323 | * Then method return status is @a osErrorISR |
kadonotakashi | 0:8fdf9a60065b | 324 | */ |
kadonotakashi | 0:8fdf9a60065b | 325 | void test_isr_calls_fail() |
kadonotakashi | 0:8fdf9a60065b | 326 | { |
kadonotakashi | 0:8fdf9a60065b | 327 | RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce); |
kadonotakashi | 0:8fdf9a60065b | 328 | |
kadonotakashi | 0:8fdf9a60065b | 329 | Ticker ticker; |
kadonotakashi | 0:8fdf9a60065b | 330 | ticker.attach(mbed::callback(rtostimer_isr_call, &rtostimer), (float) DELAY_MS / 1000.0); |
kadonotakashi | 0:8fdf9a60065b | 331 | |
kadonotakashi | 0:8fdf9a60065b | 332 | wait_ms(DELAY_MS + DELTA_MS); |
kadonotakashi | 0:8fdf9a60065b | 333 | } |
kadonotakashi | 0:8fdf9a60065b | 334 | |
kadonotakashi | 0:8fdf9a60065b | 335 | utest::v1::status_t test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 336 | { |
kadonotakashi | 0:8fdf9a60065b | 337 | GREENTEA_SETUP(10, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 338 | return verbose_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 339 | } |
kadonotakashi | 0:8fdf9a60065b | 340 | |
kadonotakashi | 0:8fdf9a60065b | 341 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 342 | Case("One-shot not restarted when elapsed", test_oneshot_not_restarted), |
kadonotakashi | 0:8fdf9a60065b | 343 | Case("Periodic repeats continuously", test_periodic_repeats), |
kadonotakashi | 0:8fdf9a60065b | 344 | Case("Stopped timer can be started again", test_start_again), |
kadonotakashi | 0:8fdf9a60065b | 345 | Case("Restart changes timeout", test_restart_updates_delay), |
kadonotakashi | 0:8fdf9a60065b | 346 | Case("Timer can be stopped", test_stop), |
kadonotakashi | 0:8fdf9a60065b | 347 | Case("Timer is created in stopped state", test_created_stopped), |
kadonotakashi | 0:8fdf9a60065b | 348 | Case("Timer started with infinite delay", test_wait_forever), |
kadonotakashi | 0:8fdf9a60065b | 349 | Case("Timer started with zero delay", test_no_wait), |
kadonotakashi | 0:8fdf9a60065b | 350 | Case("Calls from ISR fail", test_isr_calls_fail) |
kadonotakashi | 0:8fdf9a60065b | 351 | }; |
kadonotakashi | 0:8fdf9a60065b | 352 | |
kadonotakashi | 0:8fdf9a60065b | 353 | Specification specification(test_setup, cases); |
kadonotakashi | 0:8fdf9a60065b | 354 | |
kadonotakashi | 0:8fdf9a60065b | 355 | int main() |
kadonotakashi | 0:8fdf9a60065b | 356 | { |
kadonotakashi | 0:8fdf9a60065b | 357 | return !Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 358 | } |