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