-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathmain.cpp
369 lines (308 loc) · 9.42 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if !defined(MBED_CONF_RTOS_PRESENT)
#error [NOT_SUPPORTED] RTOS timer test cases require RTOS to run
#else
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "rtos.h"
using namespace utest::v1;
#define DELAY_MS 50
#define DELTA_MS 5
#define RESTART_DELAY_MS 10
#define DELAY2_MS 30
#if RESTART_DELAY_MS >= DELAY_MS
#error invalid RESTART_DELAY_MS value
#else
#if !DEVICE_USTICKER
#error [NOT_SUPPORTED] test not supported
#else
class Stopwatch: public Timer {
private:
Semaphore _sem;
public:
Stopwatch() :
Timer(), _sem(1)
{
}
~Stopwatch()
{
}
void start(void)
{
_sem.try_acquire();
Timer::start();
}
void stop(void)
{
Timer::stop();
_sem.release();
}
int32_t wait_until_stopped(uint32_t millisec = osWaitForever)
{
core_util_critical_section_enter();
int running = _running;
core_util_critical_section_exit();
if (!running) {
return 1;
}
return _sem.try_acquire_for(millisec);
}
};
void sem_callback(Semaphore *sem)
{
sem->release();
}
/** Test one-shot not restarted when elapsed
*
* Given a one-shot RtosTimer
* When the timer is started
* and given time elapses
* Then timer stops
* and elapsed time matches given delay
* and timer stays stopped
*/
void test_oneshot_not_restarted()
{
Stopwatch stopwatch;
RtosTimer rtostimer(mbed::callback(&stopwatch, &Stopwatch::stop), osTimerOnce);
stopwatch.start();
osStatus status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status);
int32_t slots = stopwatch.wait_until_stopped();
TEST_ASSERT_EQUAL(1, slots);
TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY_MS, stopwatch.read_ms());
stopwatch.start();
slots = stopwatch.wait_until_stopped(DELAY_MS + DELTA_MS);
TEST_ASSERT_EQUAL(0, slots);
#if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
#endif
}
/** Test periodic repeats continuously
*
* Given a periodic RtosTimer
* When timer is started
* and given time elapses
* Then timer repeats its operation
* When timer is stopped
* Then timer stops operation
*/
void test_periodic_repeats()
{
Stopwatch stopwatch;
RtosTimer rtostimer(mbed::callback(&stopwatch, &Stopwatch::stop), osTimerPeriodic);
stopwatch.start();
osStatus status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status);
int32_t slots = stopwatch.wait_until_stopped();
int t1 = stopwatch.read_ms();
stopwatch.reset();
stopwatch.start();
TEST_ASSERT_EQUAL(1, slots);
TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY_MS, t1);
slots = stopwatch.wait_until_stopped();
TEST_ASSERT_EQUAL(1, slots);
TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY_MS, stopwatch.read_ms());
stopwatch.start();
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osOK, status);
slots = stopwatch.wait_until_stopped(DELAY_MS + DELTA_MS);
TEST_ASSERT_EQUAL(0, slots);
#if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
#endif
}
/** Test timer can be started again
*
* Given a one-shot Rtosimer
* When the timer is started
* and given time elapses
* Then timer stops
* When the timer is started again
* and given time elapses
* Then timer stops again
*/
void test_start_again()
{
Semaphore sem(0, 1);
RtosTimer rtostimer(mbed::callback(sem_callback, &sem), osTimerOnce);
osStatus status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status);
bool acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
TEST_ASSERT(acquired);
#if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
#endif
status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status);
acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
TEST_ASSERT(acquired);
#if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
#endif
}
/** Test timer restart updates delay
*
* Given a one-shot RtosTimer
* When the timer is started
* and @a start is called again with a different delay before given time elapses
* and updated delay elapses
* Then timer stops
* and time elapsed since the second @a start call matches updated delay
*/
void test_restart_updates_delay()
{
Stopwatch stopwatch;
RtosTimer rtostimer(mbed::callback(&stopwatch, &Stopwatch::stop), osTimerOnce);
stopwatch.start();
osStatus status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status);
int32_t slots = stopwatch.wait_until_stopped(RESTART_DELAY_MS);
TEST_ASSERT_EQUAL(0, slots);
stopwatch.reset();
stopwatch.start();
status = rtostimer.start(DELAY2_MS);
TEST_ASSERT_EQUAL(osOK, status);
slots = stopwatch.wait_until_stopped();
TEST_ASSERT_EQUAL(1, slots);
TEST_ASSERT_INT_WITHIN(DELTA_MS, DELAY2_MS, stopwatch.read_ms());
#if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
#endif
}
/** Test timer is created in stopped state
*
* Given a one-shot RtosTimer
* When the timer has not been started
* Then the timer is stopped
*/
void test_created_stopped()
{
RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce);
#if !MBED_TRAP_ERRORS_ENABLED
osStatus status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
#endif
}
/** Test one-shot can be stopped
*
* Given a one-shot RtosTimer
* When the timer is started
* and timer is stopped while still running
* Then timer stops operation
*/
void test_stop()
{
Semaphore sem(0, 1);
RtosTimer rtostimer(mbed::callback(sem_callback, &sem), osTimerOnce);
osStatus status = rtostimer.start(DELAY_MS);
TEST_ASSERT_EQUAL(osOK, status);
bool acquired = sem.try_acquire_for(RESTART_DELAY_MS);
TEST_ASSERT_FALSE(acquired);
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osOK, status);
acquired = sem.try_acquire_for(DELAY_MS + DELTA_MS);
TEST_ASSERT_FALSE(acquired);
#if !MBED_TRAP_ERRORS_ENABLED
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
#endif
}
/** Test timer started with infinite delay
*
* Given a one-shot RtosTimer
* When the timer is started with @a osWaitForever delay
* Then @a start return status is @a osOK
*/
void test_wait_forever()
{
RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce);
osStatus status = rtostimer.start(osWaitForever);
TEST_ASSERT_EQUAL(osOK, status);
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osOK, status);
}
#if !MBED_TRAP_ERRORS_ENABLED
/** Test timer started with zero delay
*
* Given a one-shot RtosTimer
* When the timer is started with 0 delay
* Then @a start return status is @a osErrorParameter
*/
void test_no_wait()
{
RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce);
osStatus status = rtostimer.start(0);
TEST_ASSERT_EQUAL(osErrorParameter, status);
status = rtostimer.stop();
TEST_ASSERT_EQUAL(osErrorResource, status);
}
void rtostimer_isr_call(RtosTimer *rtostimer)
{
osStatus status = rtostimer->start(DELAY_MS);
TEST_ASSERT_EQUAL(osErrorISR, status);
status = rtostimer->stop();
TEST_ASSERT_EQUAL(osErrorISR, status);
}
/** Test timer method calls from an ISR fail
*
* Given a one-shot RtosTimer
* When a timer method is called from an ISR
* Then method return status is @a osErrorISR
*/
void test_isr_calls_fail()
{
RtosTimer rtostimer(mbed::callback(sem_callback, (Semaphore *) NULL), osTimerOnce);
Ticker ticker;
ticker.attach(mbed::callback(rtostimer_isr_call, &rtostimer), (float) DELAY_MS / 1000.0);
wait_ms(DELAY_MS + DELTA_MS);
}
#endif // !MBED_TRAP_ERRORS_ENABLED
utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("One-shot not restarted when elapsed", test_oneshot_not_restarted),
Case("Periodic repeats continuously", test_periodic_repeats),
Case("Stopped timer can be started again", test_start_again),
Case("Restart changes timeout", test_restart_updates_delay),
Case("Timer can be stopped", test_stop),
Case("Timer is created in stopped state", test_created_stopped),
Case("Timer started with infinite delay", test_wait_forever),
#if !MBED_TRAP_ERRORS_ENABLED
Case("Timer started with zero delay", test_no_wait),
Case("Calls from ISR fail", test_isr_calls_fail)
#endif
};
Specification specification(test_setup, cases);
int main()
{
return !Harness::run(specification);
}
#endif // !DEVICE_USTICKER
#endif // RESTART_DELAY_MS >= DELAY_MS
#endif