takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/events/timing/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_events.h" |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "rtos.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "unity.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "utest.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include <cstdlib> |
kadonotakashi | 0:8fdf9a60065b | 23 | #include <cmath> |
kadonotakashi | 0:8fdf9a60065b | 24 | |
kadonotakashi | 0:8fdf9a60065b | 25 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 26 | |
kadonotakashi | 0:8fdf9a60065b | 27 | #if !DEVICE_USTICKER |
kadonotakashi | 0:8fdf9a60065b | 28 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 29 | #endif |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | // Test delay |
kadonotakashi | 0:8fdf9a60065b | 32 | #ifndef TEST_EVENTS_TIMING_TIME |
kadonotakashi | 0:8fdf9a60065b | 33 | #define TEST_EVENTS_TIMING_TIME 20000 |
kadonotakashi | 0:8fdf9a60065b | 34 | #endif |
kadonotakashi | 0:8fdf9a60065b | 35 | |
kadonotakashi | 0:8fdf9a60065b | 36 | #ifndef TEST_EVENTS_TIMING_MEAN |
kadonotakashi | 0:8fdf9a60065b | 37 | #define TEST_EVENTS_TIMING_MEAN 25 |
kadonotakashi | 0:8fdf9a60065b | 38 | #endif |
kadonotakashi | 0:8fdf9a60065b | 39 | |
kadonotakashi | 0:8fdf9a60065b | 40 | #ifndef M_PI |
kadonotakashi | 0:8fdf9a60065b | 41 | #define M_PI 3.14159265358979323846264338327950288 |
kadonotakashi | 0:8fdf9a60065b | 42 | #endif |
kadonotakashi | 0:8fdf9a60065b | 43 | |
kadonotakashi | 0:8fdf9a60065b | 44 | // Random number generation to skew timing values |
kadonotakashi | 0:8fdf9a60065b | 45 | float gauss(float mu, float sigma) |
kadonotakashi | 0:8fdf9a60065b | 46 | { |
kadonotakashi | 0:8fdf9a60065b | 47 | float x = (float)rand() / ((float)RAND_MAX + 1); |
kadonotakashi | 0:8fdf9a60065b | 48 | float y = (float)rand() / ((float)RAND_MAX + 1); |
kadonotakashi | 0:8fdf9a60065b | 49 | float x2pi = x * 2.0 * M_PI; |
kadonotakashi | 0:8fdf9a60065b | 50 | float g2rad = sqrt(-2.0 * log(1.0 - y)); |
kadonotakashi | 0:8fdf9a60065b | 51 | float z = cos(x2pi) * g2rad; |
kadonotakashi | 0:8fdf9a60065b | 52 | return mu + z * sigma; |
kadonotakashi | 0:8fdf9a60065b | 53 | } |
kadonotakashi | 0:8fdf9a60065b | 54 | |
kadonotakashi | 0:8fdf9a60065b | 55 | float chisq(float sigma) |
kadonotakashi | 0:8fdf9a60065b | 56 | { |
kadonotakashi | 0:8fdf9a60065b | 57 | return pow(gauss(0, sqrt(sigma)), 2); |
kadonotakashi | 0:8fdf9a60065b | 58 | } |
kadonotakashi | 0:8fdf9a60065b | 59 | |
kadonotakashi | 0:8fdf9a60065b | 60 | |
kadonotakashi | 0:8fdf9a60065b | 61 | Timer timer; |
kadonotakashi | 0:8fdf9a60065b | 62 | DigitalOut led(LED1); |
kadonotakashi | 0:8fdf9a60065b | 63 | |
kadonotakashi | 0:8fdf9a60065b | 64 | equeue_sema_t sema; |
kadonotakashi | 0:8fdf9a60065b | 65 | |
kadonotakashi | 0:8fdf9a60065b | 66 | // Timer timing test |
kadonotakashi | 0:8fdf9a60065b | 67 | void timer_timing_test() |
kadonotakashi | 0:8fdf9a60065b | 68 | { |
kadonotakashi | 0:8fdf9a60065b | 69 | timer.reset(); |
kadonotakashi | 0:8fdf9a60065b | 70 | timer.start(); |
kadonotakashi | 0:8fdf9a60065b | 71 | int prev = timer.read_us(); |
kadonotakashi | 0:8fdf9a60065b | 72 | |
kadonotakashi | 0:8fdf9a60065b | 73 | while (prev < TEST_EVENTS_TIMING_TIME * 1000) { |
kadonotakashi | 0:8fdf9a60065b | 74 | int next = timer.read_us(); |
kadonotakashi | 0:8fdf9a60065b | 75 | if (next < prev) { |
kadonotakashi | 0:8fdf9a60065b | 76 | printf("backwards drift %d -> %d (%08x -> %08x)\r\n", |
kadonotakashi | 0:8fdf9a60065b | 77 | prev, next, prev, next); |
kadonotakashi | 0:8fdf9a60065b | 78 | } |
kadonotakashi | 0:8fdf9a60065b | 79 | TEST_ASSERT(next >= prev); |
kadonotakashi | 0:8fdf9a60065b | 80 | prev = next; |
kadonotakashi | 0:8fdf9a60065b | 81 | } |
kadonotakashi | 0:8fdf9a60065b | 82 | } |
kadonotakashi | 0:8fdf9a60065b | 83 | |
kadonotakashi | 0:8fdf9a60065b | 84 | // equeue tick timing test |
kadonotakashi | 0:8fdf9a60065b | 85 | void tick_timing_test() |
kadonotakashi | 0:8fdf9a60065b | 86 | { |
kadonotakashi | 0:8fdf9a60065b | 87 | unsigned start = equeue_tick(); |
kadonotakashi | 0:8fdf9a60065b | 88 | int prev = 0; |
kadonotakashi | 0:8fdf9a60065b | 89 | |
kadonotakashi | 0:8fdf9a60065b | 90 | while (prev < TEST_EVENTS_TIMING_TIME) { |
kadonotakashi | 0:8fdf9a60065b | 91 | int next = equeue_tick() - start; |
kadonotakashi | 0:8fdf9a60065b | 92 | if (next < prev) { |
kadonotakashi | 0:8fdf9a60065b | 93 | printf("backwards drift %d -> %d (%08x -> %08x)\r\n", |
kadonotakashi | 0:8fdf9a60065b | 94 | prev, next, prev, next); |
kadonotakashi | 0:8fdf9a60065b | 95 | } |
kadonotakashi | 0:8fdf9a60065b | 96 | TEST_ASSERT(next >= prev); |
kadonotakashi | 0:8fdf9a60065b | 97 | prev = next; |
kadonotakashi | 0:8fdf9a60065b | 98 | } |
kadonotakashi | 0:8fdf9a60065b | 99 | } |
kadonotakashi | 0:8fdf9a60065b | 100 | |
kadonotakashi | 0:8fdf9a60065b | 101 | // equeue semaphore timing test |
kadonotakashi | 0:8fdf9a60065b | 102 | void semaphore_timing_test() |
kadonotakashi | 0:8fdf9a60065b | 103 | { |
kadonotakashi | 0:8fdf9a60065b | 104 | srand(0); |
kadonotakashi | 0:8fdf9a60065b | 105 | timer.reset(); |
kadonotakashi | 0:8fdf9a60065b | 106 | timer.start(); |
kadonotakashi | 0:8fdf9a60065b | 107 | |
kadonotakashi | 0:8fdf9a60065b | 108 | int err = equeue_sema_create(&sema); |
kadonotakashi | 0:8fdf9a60065b | 109 | TEST_ASSERT_EQUAL(0, err); |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | while (timer.read_ms() < TEST_EVENTS_TIMING_TIME) { |
kadonotakashi | 0:8fdf9a60065b | 112 | int delay = chisq(TEST_EVENTS_TIMING_MEAN); |
kadonotakashi | 0:8fdf9a60065b | 113 | |
kadonotakashi | 0:8fdf9a60065b | 114 | int start = timer.read_us(); |
kadonotakashi | 0:8fdf9a60065b | 115 | equeue_sema_wait(&sema, delay); |
kadonotakashi | 0:8fdf9a60065b | 116 | int taken = timer.read_us() - start; |
kadonotakashi | 0:8fdf9a60065b | 117 | |
kadonotakashi | 0:8fdf9a60065b | 118 | if (taken < (delay * 1000 - 5000) || taken > (delay * 1000 + 5000)) { |
kadonotakashi | 0:8fdf9a60065b | 119 | printf("delay %dms => error %dus\r\n", delay, abs(1000 * delay - taken)); |
kadonotakashi | 0:8fdf9a60065b | 120 | } |
kadonotakashi | 0:8fdf9a60065b | 121 | |
kadonotakashi | 0:8fdf9a60065b | 122 | TEST_ASSERT_INT_WITHIN(5000, taken, delay * 1000); |
kadonotakashi | 0:8fdf9a60065b | 123 | |
kadonotakashi | 0:8fdf9a60065b | 124 | led = !led; |
kadonotakashi | 0:8fdf9a60065b | 125 | } |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | equeue_sema_destroy(&sema); |
kadonotakashi | 0:8fdf9a60065b | 128 | } |
kadonotakashi | 0:8fdf9a60065b | 129 | |
kadonotakashi | 0:8fdf9a60065b | 130 | |
kadonotakashi | 0:8fdf9a60065b | 131 | // Test setup |
kadonotakashi | 0:8fdf9a60065b | 132 | utest::v1::status_t test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 133 | { |
kadonotakashi | 0:8fdf9a60065b | 134 | GREENTEA_SETUP((number_of_cases + 1)*TEST_EVENTS_TIMING_TIME / 1000, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 135 | return verbose_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 136 | } |
kadonotakashi | 0:8fdf9a60065b | 137 | |
kadonotakashi | 0:8fdf9a60065b | 138 | const Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 139 | Case("Testing accuracy of timer", timer_timing_test), |
kadonotakashi | 0:8fdf9a60065b | 140 | Case("Testing accuracy of equeue tick", tick_timing_test), |
kadonotakashi | 0:8fdf9a60065b | 141 | Case("Testing accuracy of equeue semaphore", semaphore_timing_test), |
kadonotakashi | 0:8fdf9a60065b | 142 | }; |
kadonotakashi | 0:8fdf9a60065b | 143 | |
kadonotakashi | 0:8fdf9a60065b | 144 | Specification specification(test_setup, cases); |
kadonotakashi | 0:8fdf9a60065b | 145 | |
kadonotakashi | 0:8fdf9a60065b | 146 | int main() |
kadonotakashi | 0:8fdf9a60065b | 147 | { |
kadonotakashi | 0:8fdf9a60065b | 148 | return !Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 149 | } |
kadonotakashi | 0:8fdf9a60065b | 150 |