Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

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?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 /*
kadonotakashi 0:8fdf9a60065b 2 * Copyright (c) 2017, ARM Limited, All Rights Reserved
kadonotakashi 0:8fdf9a60065b 3 * SPDX-License-Identifier: Apache-2.0
kadonotakashi 0:8fdf9a60065b 4 *
kadonotakashi 0:8fdf9a60065b 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kadonotakashi 0:8fdf9a60065b 6 * not use this file except in compliance with the License.
kadonotakashi 0:8fdf9a60065b 7 * You may obtain a copy of the License at
kadonotakashi 0:8fdf9a60065b 8 *
kadonotakashi 0:8fdf9a60065b 9 * http://www.apache.org/licenses/LICENSE-2.0
kadonotakashi 0:8fdf9a60065b 10 *
kadonotakashi 0:8fdf9a60065b 11 * Unless required by applicable law or agreed to in writing, software
kadonotakashi 0:8fdf9a60065b 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kadonotakashi 0:8fdf9a60065b 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kadonotakashi 0:8fdf9a60065b 14 * See the License for the specific language governing permissions and
kadonotakashi 0:8fdf9a60065b 15 * limitations under the License.
kadonotakashi 0:8fdf9a60065b 16 */
kadonotakashi 0:8fdf9a60065b 17
kadonotakashi 0:8fdf9a60065b 18 #include "mbed.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 "rtos.h"
kadonotakashi 0:8fdf9a60065b 23 #include "hal/us_ticker_api.h"
kadonotakashi 0:8fdf9a60065b 24
kadonotakashi 0:8fdf9a60065b 25 #if !DEVICE_USTICKER
kadonotakashi 0:8fdf9a60065b 26 #error [NOT_SUPPORTED] test not supported
kadonotakashi 0:8fdf9a60065b 27 #endif
kadonotakashi 0:8fdf9a60065b 28
kadonotakashi 0:8fdf9a60065b 29 using namespace utest::v1;
kadonotakashi 0:8fdf9a60065b 30
kadonotakashi 0:8fdf9a60065b 31 extern uint32_t SystemCoreClock;
kadonotakashi 0:8fdf9a60065b 32
kadonotakashi 0:8fdf9a60065b 33 #define US_PER_SEC 1000000
kadonotakashi 0:8fdf9a60065b 34 #define US_PER_MSEC 1000
kadonotakashi 0:8fdf9a60065b 35 #define MSEC_PER_SEC 1000
kadonotakashi 0:8fdf9a60065b 36
kadonotakashi 0:8fdf9a60065b 37 /*
kadonotakashi 0:8fdf9a60065b 38 * Define tolerance as follows:
kadonotakashi 0:8fdf9a60065b 39 * tolerance = 500 us + 2% of measured time
kadonotakashi 0:8fdf9a60065b 40 *
kadonotakashi 0:8fdf9a60065b 41 * e.g.
kadonotakashi 0:8fdf9a60065b 42 * 1 ms delay: tolerance = 520 us
kadonotakashi 0:8fdf9a60065b 43 * 10 ms delay: tolerance = 700 us
kadonotakashi 0:8fdf9a60065b 44 * 100 ms delay: tolerance = 2500 us
kadonotakashi 0:8fdf9a60065b 45 * 1000 ms delay: tolerance = 20500 us
kadonotakashi 0:8fdf9a60065b 46 *
kadonotakashi 0:8fdf9a60065b 47 * */
kadonotakashi 0:8fdf9a60065b 48 #ifdef NO_SYSTICK
kadonotakashi 0:8fdf9a60065b 49 #define TOLERANCE 5
kadonotakashi 0:8fdf9a60065b 50 #else
kadonotakashi 0:8fdf9a60065b 51 #define TOLERANCE 2
kadonotakashi 0:8fdf9a60065b 52 #endif
kadonotakashi 0:8fdf9a60065b 53
kadonotakashi 0:8fdf9a60065b 54 #define DELTA_US(delay_ms) (500 + (delay_ms) * US_PER_MSEC * TOLERANCE / 100)
kadonotakashi 0:8fdf9a60065b 55 #define DELTA_MS(delay_ms) (1 + (delay_ms) * TOLERANCE / 100)
kadonotakashi 0:8fdf9a60065b 56 #define DELTA_S(delay_ms) (0.000500f + ((float)(delay_ms)) * ((float)(TOLERANCE) / 100.f) / MSEC_PER_SEC)
kadonotakashi 0:8fdf9a60065b 57
kadonotakashi 0:8fdf9a60065b 58 #define TICKER_FREQ_1MHZ 1000000
kadonotakashi 0:8fdf9a60065b 59 #define TICKER_BITS 32
kadonotakashi 0:8fdf9a60065b 60
kadonotakashi 0:8fdf9a60065b 61 static Timer *p_timer = NULL;
kadonotakashi 0:8fdf9a60065b 62
kadonotakashi 0:8fdf9a60065b 63 /* Global variable used to simulate passage of time
kadonotakashi 0:8fdf9a60065b 64 * in case when timer which uses user ticker is tested.
kadonotakashi 0:8fdf9a60065b 65 */
kadonotakashi 0:8fdf9a60065b 66 static uint32_t curr_ticker_ticks_val;
kadonotakashi 0:8fdf9a60065b 67
kadonotakashi 0:8fdf9a60065b 68
kadonotakashi 0:8fdf9a60065b 69 /* Replacement for generic wait functions to avoid invoking OS scheduling stuff. */
kadonotakashi 0:8fdf9a60065b 70 void busy_wait_us(int us)
kadonotakashi 0:8fdf9a60065b 71 {
kadonotakashi 0:8fdf9a60065b 72 const ticker_data_t *const ticker = get_us_ticker_data();
kadonotakashi 0:8fdf9a60065b 73 uint32_t start = ticker_read(ticker);
kadonotakashi 0:8fdf9a60065b 74 while ((ticker_read(ticker) - start) < (uint32_t)us);
kadonotakashi 0:8fdf9a60065b 75 }
kadonotakashi 0:8fdf9a60065b 76
kadonotakashi 0:8fdf9a60065b 77 void busy_wait_ms(int ms)
kadonotakashi 0:8fdf9a60065b 78 {
kadonotakashi 0:8fdf9a60065b 79 busy_wait_us(ms * US_PER_MSEC);
kadonotakashi 0:8fdf9a60065b 80 }
kadonotakashi 0:8fdf9a60065b 81
kadonotakashi 0:8fdf9a60065b 82 /* User ticker interface function. */
kadonotakashi 0:8fdf9a60065b 83 static void stub_interface_init()
kadonotakashi 0:8fdf9a60065b 84 {
kadonotakashi 0:8fdf9a60065b 85 /* do nothing. */
kadonotakashi 0:8fdf9a60065b 86 }
kadonotakashi 0:8fdf9a60065b 87
kadonotakashi 0:8fdf9a60065b 88 /* User ticker interface function - only this
kadonotakashi 0:8fdf9a60065b 89 * ticker interface function is used by Timer API. */
kadonotakashi 0:8fdf9a60065b 90 static uint32_t stub_ticker_read(void)
kadonotakashi 0:8fdf9a60065b 91 {
kadonotakashi 0:8fdf9a60065b 92 /* Simulate elapsed time. */
kadonotakashi 0:8fdf9a60065b 93 return curr_ticker_ticks_val;
kadonotakashi 0:8fdf9a60065b 94 }
kadonotakashi 0:8fdf9a60065b 95
kadonotakashi 0:8fdf9a60065b 96 /* User ticker interface function. */
kadonotakashi 0:8fdf9a60065b 97 static void stub_disable_interrupt(void)
kadonotakashi 0:8fdf9a60065b 98 {
kadonotakashi 0:8fdf9a60065b 99 /* do nothing. */
kadonotakashi 0:8fdf9a60065b 100 }
kadonotakashi 0:8fdf9a60065b 101
kadonotakashi 0:8fdf9a60065b 102 /* User ticker interface function. */
kadonotakashi 0:8fdf9a60065b 103 static void stub_clear_interrupt(void)
kadonotakashi 0:8fdf9a60065b 104 {
kadonotakashi 0:8fdf9a60065b 105 /* do nothing. */
kadonotakashi 0:8fdf9a60065b 106 }
kadonotakashi 0:8fdf9a60065b 107
kadonotakashi 0:8fdf9a60065b 108 /* User ticker interface function. */
kadonotakashi 0:8fdf9a60065b 109 static void stub_set_interrupt(timestamp_t timestamp)
kadonotakashi 0:8fdf9a60065b 110 {
kadonotakashi 0:8fdf9a60065b 111 /* do nothing. */
kadonotakashi 0:8fdf9a60065b 112 }
kadonotakashi 0:8fdf9a60065b 113
kadonotakashi 0:8fdf9a60065b 114 /* User ticker interface function. */
kadonotakashi 0:8fdf9a60065b 115 static void stub_fire_interrupt(void)
kadonotakashi 0:8fdf9a60065b 116 {
kadonotakashi 0:8fdf9a60065b 117 /* do nothing. */
kadonotakashi 0:8fdf9a60065b 118 }
kadonotakashi 0:8fdf9a60065b 119
kadonotakashi 0:8fdf9a60065b 120 /* User ticker interface function. */
kadonotakashi 0:8fdf9a60065b 121 static void stub_free(void)
kadonotakashi 0:8fdf9a60065b 122 {
kadonotakashi 0:8fdf9a60065b 123 /* do nothing. */
kadonotakashi 0:8fdf9a60065b 124 }
kadonotakashi 0:8fdf9a60065b 125
kadonotakashi 0:8fdf9a60065b 126 ticker_info_t info =
kadonotakashi 0:8fdf9a60065b 127 { TICKER_FREQ_1MHZ, TICKER_BITS };
kadonotakashi 0:8fdf9a60065b 128
kadonotakashi 0:8fdf9a60065b 129 const ticker_info_t *stub_get_info(void)
kadonotakashi 0:8fdf9a60065b 130 {
kadonotakashi 0:8fdf9a60065b 131 return &info;
kadonotakashi 0:8fdf9a60065b 132 }
kadonotakashi 0:8fdf9a60065b 133
kadonotakashi 0:8fdf9a60065b 134 /* User ticker event queue. */
kadonotakashi 0:8fdf9a60065b 135 static ticker_event_queue_t my_events = { 0 };
kadonotakashi 0:8fdf9a60065b 136
kadonotakashi 0:8fdf9a60065b 137 /* User ticker interface data. */
kadonotakashi 0:8fdf9a60065b 138 static const ticker_interface_t us_interface = {
kadonotakashi 0:8fdf9a60065b 139 .init = stub_interface_init,
kadonotakashi 0:8fdf9a60065b 140 .read = stub_ticker_read, /* Only this function is used by the Timer. */
kadonotakashi 0:8fdf9a60065b 141 .disable_interrupt = stub_disable_interrupt,
kadonotakashi 0:8fdf9a60065b 142 .clear_interrupt = stub_clear_interrupt,
kadonotakashi 0:8fdf9a60065b 143 .set_interrupt = stub_set_interrupt,
kadonotakashi 0:8fdf9a60065b 144 .fire_interrupt = stub_fire_interrupt,
kadonotakashi 0:8fdf9a60065b 145 .free = stub_free,
kadonotakashi 0:8fdf9a60065b 146 .get_info = stub_get_info,
kadonotakashi 0:8fdf9a60065b 147 };
kadonotakashi 0:8fdf9a60065b 148
kadonotakashi 0:8fdf9a60065b 149 /* User ticker data structure. */
kadonotakashi 0:8fdf9a60065b 150 static const ticker_data_t us_data = {
kadonotakashi 0:8fdf9a60065b 151 .interface = &us_interface,
kadonotakashi 0:8fdf9a60065b 152 .queue = &my_events
kadonotakashi 0:8fdf9a60065b 153 };
kadonotakashi 0:8fdf9a60065b 154
kadonotakashi 0:8fdf9a60065b 155 /* Function which returns user ticker data. */
kadonotakashi 0:8fdf9a60065b 156 const ticker_data_t *get_user_ticker_data(void)
kadonotakashi 0:8fdf9a60065b 157 {
kadonotakashi 0:8fdf9a60065b 158 return &us_data;
kadonotakashi 0:8fdf9a60065b 159 }
kadonotakashi 0:8fdf9a60065b 160
kadonotakashi 0:8fdf9a60065b 161 /* Initialisation of the Timer object which uses
kadonotakashi 0:8fdf9a60065b 162 * ticker data provided by the user.
kadonotakashi 0:8fdf9a60065b 163 *
kadonotakashi 0:8fdf9a60065b 164 * */
kadonotakashi 0:8fdf9a60065b 165 utest::v1::status_t timer_user_ticker_setup_handler(const Case *const source, const size_t index_of_case)
kadonotakashi 0:8fdf9a60065b 166 {
kadonotakashi 0:8fdf9a60065b 167 p_timer = new Timer(get_user_ticker_data());
kadonotakashi 0:8fdf9a60065b 168
kadonotakashi 0:8fdf9a60065b 169 /* Check if Timer object has been created. */
kadonotakashi 0:8fdf9a60065b 170 TEST_ASSERT_NOT_NULL(p_timer);
kadonotakashi 0:8fdf9a60065b 171
kadonotakashi 0:8fdf9a60065b 172 return greentea_case_setup_handler(source, index_of_case);
kadonotakashi 0:8fdf9a60065b 173 }
kadonotakashi 0:8fdf9a60065b 174
kadonotakashi 0:8fdf9a60065b 175 /* Initialisation of the Timer object which uses
kadonotakashi 0:8fdf9a60065b 176 * default os ticker data.
kadonotakashi 0:8fdf9a60065b 177 *
kadonotakashi 0:8fdf9a60065b 178 * */
kadonotakashi 0:8fdf9a60065b 179 utest::v1::status_t timer_os_ticker_setup_handler(const Case *const source, const size_t index_of_case)
kadonotakashi 0:8fdf9a60065b 180 {
kadonotakashi 0:8fdf9a60065b 181 p_timer = new Timer();
kadonotakashi 0:8fdf9a60065b 182
kadonotakashi 0:8fdf9a60065b 183 /* Check if Timer object has been created. */
kadonotakashi 0:8fdf9a60065b 184 TEST_ASSERT_NOT_NULL(p_timer);
kadonotakashi 0:8fdf9a60065b 185
kadonotakashi 0:8fdf9a60065b 186 return greentea_case_setup_handler(source, index_of_case);
kadonotakashi 0:8fdf9a60065b 187 }
kadonotakashi 0:8fdf9a60065b 188
kadonotakashi 0:8fdf9a60065b 189 /* Test finalisation.
kadonotakashi 0:8fdf9a60065b 190 *
kadonotakashi 0:8fdf9a60065b 191 * */
kadonotakashi 0:8fdf9a60065b 192 utest::v1::status_t cleanup_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t reason)
kadonotakashi 0:8fdf9a60065b 193 {
kadonotakashi 0:8fdf9a60065b 194 delete p_timer;
kadonotakashi 0:8fdf9a60065b 195
kadonotakashi 0:8fdf9a60065b 196 p_timer = NULL;
kadonotakashi 0:8fdf9a60065b 197
kadonotakashi 0:8fdf9a60065b 198 return greentea_case_teardown_handler(source, passed, failed, reason);
kadonotakashi 0:8fdf9a60065b 199 }
kadonotakashi 0:8fdf9a60065b 200
kadonotakashi 0:8fdf9a60065b 201 /* This test verifies if timer is stopped after
kadonotakashi 0:8fdf9a60065b 202 * creation.
kadonotakashi 0:8fdf9a60065b 203 *
kadonotakashi 0:8fdf9a60065b 204 * Note: this function assumes that Timer uses os ticker.
kadonotakashi 0:8fdf9a60065b 205 *
kadonotakashi 0:8fdf9a60065b 206 * Given Timer has been successfully created.
kadonotakashi 0:8fdf9a60065b 207 * When read of timer elapsed time is requested.
kadonotakashi 0:8fdf9a60065b 208 * Then result is always 0.
kadonotakashi 0:8fdf9a60065b 209 */
kadonotakashi 0:8fdf9a60065b 210 void test_timer_creation_os_ticker()
kadonotakashi 0:8fdf9a60065b 211 {
kadonotakashi 0:8fdf9a60065b 212 /* Check results. */
kadonotakashi 0:8fdf9a60065b 213 TEST_ASSERT_EQUAL_FLOAT(0, p_timer->read());
kadonotakashi 0:8fdf9a60065b 214 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 215 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 216 TEST_ASSERT_EQUAL_UINT64(0, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 217
kadonotakashi 0:8fdf9a60065b 218 /* Wait 10 ms.
kadonotakashi 0:8fdf9a60065b 219 * After that operation timer read routines should still return 0. */
kadonotakashi 0:8fdf9a60065b 220 busy_wait_ms(10);
kadonotakashi 0:8fdf9a60065b 221
kadonotakashi 0:8fdf9a60065b 222 /* Check results. */
kadonotakashi 0:8fdf9a60065b 223 TEST_ASSERT_EQUAL_FLOAT(0, p_timer->read());
kadonotakashi 0:8fdf9a60065b 224 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 225 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 226 TEST_ASSERT_EQUAL_UINT64(0, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 227 }
kadonotakashi 0:8fdf9a60065b 228
kadonotakashi 0:8fdf9a60065b 229 /* This test verifies if timer is stopped after
kadonotakashi 0:8fdf9a60065b 230 * creation.
kadonotakashi 0:8fdf9a60065b 231 *
kadonotakashi 0:8fdf9a60065b 232 * Note: this function assumes that Timer uses user/fake ticker
kadonotakashi 0:8fdf9a60065b 233 * which returns time value provided in curr_ticker_ticks_val
kadonotakashi 0:8fdf9a60065b 234 * global variable.
kadonotakashi 0:8fdf9a60065b 235 *
kadonotakashi 0:8fdf9a60065b 236 * Given Timer has been successfully created.
kadonotakashi 0:8fdf9a60065b 237 * When read of timer elapsed time is requested.
kadonotakashi 0:8fdf9a60065b 238 * Then result is always 0.
kadonotakashi 0:8fdf9a60065b 239 */
kadonotakashi 0:8fdf9a60065b 240 void test_timer_creation_user_ticker()
kadonotakashi 0:8fdf9a60065b 241 {
kadonotakashi 0:8fdf9a60065b 242 /* For timer which is using user ticker simulate timer
kadonotakashi 0:8fdf9a60065b 243 * creation time (irrelevant in case of os ticker). */
kadonotakashi 0:8fdf9a60065b 244 curr_ticker_ticks_val = 10000;
kadonotakashi 0:8fdf9a60065b 245
kadonotakashi 0:8fdf9a60065b 246 /* Check results. */
kadonotakashi 0:8fdf9a60065b 247 TEST_ASSERT_EQUAL_FLOAT(0, p_timer->read());
kadonotakashi 0:8fdf9a60065b 248 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 249 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 250 TEST_ASSERT_EQUAL_UINT64(0, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 251
kadonotakashi 0:8fdf9a60065b 252 /* Simulate that 10 ms has elapsed.
kadonotakashi 0:8fdf9a60065b 253 * After that operation timer read routines should still return 0. */
kadonotakashi 0:8fdf9a60065b 254 curr_ticker_ticks_val += 10000;
kadonotakashi 0:8fdf9a60065b 255
kadonotakashi 0:8fdf9a60065b 256 /* Check results. */
kadonotakashi 0:8fdf9a60065b 257 TEST_ASSERT_EQUAL_FLOAT(0, p_timer->read());
kadonotakashi 0:8fdf9a60065b 258 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 259 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 260 TEST_ASSERT_EQUAL_UINT64(0, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 261 }
kadonotakashi 0:8fdf9a60065b 262
kadonotakashi 0:8fdf9a60065b 263 /* This test verifies verifies if read(), read_us(), read_ms(),
kadonotakashi 0:8fdf9a60065b 264 * read_high_resolution_us() functions returns valid values.
kadonotakashi 0:8fdf9a60065b 265 *
kadonotakashi 0:8fdf9a60065b 266 * Note: this function assumes that Timer uses user/fake ticker
kadonotakashi 0:8fdf9a60065b 267 * which returns time value provided in curr_ticker_ticks_val
kadonotakashi 0:8fdf9a60065b 268 * global variable.
kadonotakashi 0:8fdf9a60065b 269 *
kadonotakashi 0:8fdf9a60065b 270 * Given Timer has been successfully created and
kadonotakashi 0:8fdf9a60065b 271 * few times started and stopped after a specified period of time.
kadonotakashi 0:8fdf9a60065b 272 * When timer read request is performed.
kadonotakashi 0:8fdf9a60065b 273 * Then read functions return accumulated time elapsed between starts
kadonotakashi 0:8fdf9a60065b 274 * and stops.
kadonotakashi 0:8fdf9a60065b 275 */
kadonotakashi 0:8fdf9a60065b 276 void test_timer_time_accumulation_user_ticker()
kadonotakashi 0:8fdf9a60065b 277 {
kadonotakashi 0:8fdf9a60065b 278 /* Simulate that current time is equal to 0 us. */
kadonotakashi 0:8fdf9a60065b 279 curr_ticker_ticks_val = 0;
kadonotakashi 0:8fdf9a60065b 280
kadonotakashi 0:8fdf9a60065b 281 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 282 p_timer->start();
kadonotakashi 0:8fdf9a60065b 283
kadonotakashi 0:8fdf9a60065b 284 /* -- Simulate that current time is equal to 1 us -- */
kadonotakashi 0:8fdf9a60065b 285 curr_ticker_ticks_val = 1;
kadonotakashi 0:8fdf9a60065b 286
kadonotakashi 0:8fdf9a60065b 287 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 288 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 289
kadonotakashi 0:8fdf9a60065b 290 /* Check results - 1 us has elapsed. */
kadonotakashi 0:8fdf9a60065b 291 TEST_ASSERT_EQUAL_FLOAT(0.000001f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 292 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 293 TEST_ASSERT_EQUAL_INT32(1, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 294 TEST_ASSERT_EQUAL_UINT64(1, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 295
kadonotakashi 0:8fdf9a60065b 296 /* Simulate that 100 us has elapsed between stop and start. */
kadonotakashi 0:8fdf9a60065b 297 curr_ticker_ticks_val = 101;
kadonotakashi 0:8fdf9a60065b 298
kadonotakashi 0:8fdf9a60065b 299 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 300 p_timer->start();
kadonotakashi 0:8fdf9a60065b 301
kadonotakashi 0:8fdf9a60065b 302 /* -- Simulate that current time is equal to 225 us -- */
kadonotakashi 0:8fdf9a60065b 303 curr_ticker_ticks_val = 225;
kadonotakashi 0:8fdf9a60065b 304
kadonotakashi 0:8fdf9a60065b 305 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 306 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 307
kadonotakashi 0:8fdf9a60065b 308 /* Check results - 125 us have elapsed. */
kadonotakashi 0:8fdf9a60065b 309 TEST_ASSERT_EQUAL_FLOAT(0.000125f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 310 TEST_ASSERT_EQUAL_INT32(0, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 311 TEST_ASSERT_EQUAL_INT32(125, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 312 TEST_ASSERT_EQUAL_UINT64(125, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 313
kadonotakashi 0:8fdf9a60065b 314 /* Simulate that 100 us has elapsed between stop and start. */
kadonotakashi 0:8fdf9a60065b 315 curr_ticker_ticks_val = 325;
kadonotakashi 0:8fdf9a60065b 316
kadonotakashi 0:8fdf9a60065b 317 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 318 p_timer->start();
kadonotakashi 0:8fdf9a60065b 319
kadonotakashi 0:8fdf9a60065b 320 /* -- Simulate that current time is equal to 1200 us -- */
kadonotakashi 0:8fdf9a60065b 321 curr_ticker_ticks_val = 1200;
kadonotakashi 0:8fdf9a60065b 322
kadonotakashi 0:8fdf9a60065b 323 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 324 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 325
kadonotakashi 0:8fdf9a60065b 326 /* Check results - 1 ms has elapsed. */
kadonotakashi 0:8fdf9a60065b 327 TEST_ASSERT_EQUAL_FLOAT(0.001000f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 328 TEST_ASSERT_EQUAL_INT32(1, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 329 TEST_ASSERT_EQUAL_INT32(1000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 330 TEST_ASSERT_EQUAL_UINT64(1000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 331
kadonotakashi 0:8fdf9a60065b 332 /* Simulate that 100 us has elapsed between stop and start. */
kadonotakashi 0:8fdf9a60065b 333 curr_ticker_ticks_val = 1300;
kadonotakashi 0:8fdf9a60065b 334
kadonotakashi 0:8fdf9a60065b 335 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 336 p_timer->start();
kadonotakashi 0:8fdf9a60065b 337
kadonotakashi 0:8fdf9a60065b 338 /* -- Simulate that current time is equal to 125300 us -- */
kadonotakashi 0:8fdf9a60065b 339 curr_ticker_ticks_val = 125300;
kadonotakashi 0:8fdf9a60065b 340
kadonotakashi 0:8fdf9a60065b 341 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 342 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 343
kadonotakashi 0:8fdf9a60065b 344 /* Check results - 125 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 345 TEST_ASSERT_EQUAL_FLOAT(0.125000f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 346 TEST_ASSERT_EQUAL_INT32(125, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 347 TEST_ASSERT_EQUAL_INT32(125000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 348 TEST_ASSERT_EQUAL_UINT64(125000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 349
kadonotakashi 0:8fdf9a60065b 350 /* Simulate that 100 us has elapsed between stop and start. */
kadonotakashi 0:8fdf9a60065b 351 curr_ticker_ticks_val = 125400;
kadonotakashi 0:8fdf9a60065b 352
kadonotakashi 0:8fdf9a60065b 353 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 354 p_timer->start();
kadonotakashi 0:8fdf9a60065b 355
kadonotakashi 0:8fdf9a60065b 356 /* -- Simulate that current time is equal to 1000400 us -- */
kadonotakashi 0:8fdf9a60065b 357 curr_ticker_ticks_val = 1000400;
kadonotakashi 0:8fdf9a60065b 358
kadonotakashi 0:8fdf9a60065b 359 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 360 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 361
kadonotakashi 0:8fdf9a60065b 362 /* Check results - 1 s has elapsed. */
kadonotakashi 0:8fdf9a60065b 363 TEST_ASSERT_EQUAL_FLOAT(1.000000f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 364 TEST_ASSERT_EQUAL_INT32(1000, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 365 TEST_ASSERT_EQUAL_INT32(1000000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 366 TEST_ASSERT_EQUAL_UINT64(1000000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 367
kadonotakashi 0:8fdf9a60065b 368 /* Simulate that 100 us has elapsed between stop and start. */
kadonotakashi 0:8fdf9a60065b 369 curr_ticker_ticks_val = 1000500;
kadonotakashi 0:8fdf9a60065b 370
kadonotakashi 0:8fdf9a60065b 371 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 372 p_timer->start();
kadonotakashi 0:8fdf9a60065b 373
kadonotakashi 0:8fdf9a60065b 374 /* -- Simulate that current time is equal to 125000500 us -- */
kadonotakashi 0:8fdf9a60065b 375 curr_ticker_ticks_val = 125000500;
kadonotakashi 0:8fdf9a60065b 376
kadonotakashi 0:8fdf9a60065b 377 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 378 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 379
kadonotakashi 0:8fdf9a60065b 380 /* Check results - 125 s have elapsed. */
kadonotakashi 0:8fdf9a60065b 381 TEST_ASSERT_EQUAL_FLOAT(125.000000f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 382 TEST_ASSERT_EQUAL_INT32(125000, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 383 TEST_ASSERT_EQUAL_INT32(125000000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 384 TEST_ASSERT_EQUAL_UINT64(125000000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 385
kadonotakashi 0:8fdf9a60065b 386 /* Simulate that 100 us has elapsed between stop and start. */
kadonotakashi 0:8fdf9a60065b 387 curr_ticker_ticks_val = 125000600;
kadonotakashi 0:8fdf9a60065b 388
kadonotakashi 0:8fdf9a60065b 389 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 390 p_timer->start();
kadonotakashi 0:8fdf9a60065b 391
kadonotakashi 0:8fdf9a60065b 392 /* -- Simulate that current time is equal to MAX_INT_32 us + 600 us (delays
kadonotakashi 0:8fdf9a60065b 393 * between stops and starts) -- */
kadonotakashi 0:8fdf9a60065b 394
kadonotakashi 0:8fdf9a60065b 395 /* Note that ticker is based on unsigned 32-bit int microsecond counters
kadonotakashi 0:8fdf9a60065b 396 * while timers are based on 32-bit signed int microsecond counters,
kadonotakashi 0:8fdf9a60065b 397 * so timers can only count up to a maximum of 2^31-1 microseconds i.e.
kadonotakashi 0:8fdf9a60065b 398 * 2147483647 us (about 35 minutes). */
kadonotakashi 0:8fdf9a60065b 399 curr_ticker_ticks_val = 2147484247;
kadonotakashi 0:8fdf9a60065b 400
kadonotakashi 0:8fdf9a60065b 401 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 402 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 403
kadonotakashi 0:8fdf9a60065b 404 /* Check results - 2147483647 (MAX_INT_32) us have elapsed. */
kadonotakashi 0:8fdf9a60065b 405 TEST_ASSERT_EQUAL_FLOAT(2147.483647f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 406 TEST_ASSERT_EQUAL_INT32(2147483, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 407 TEST_ASSERT_EQUAL_INT32(2147483647, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 408 TEST_ASSERT_EQUAL_UINT64(2147483647, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 409 }
kadonotakashi 0:8fdf9a60065b 410
kadonotakashi 0:8fdf9a60065b 411 /* This test verifies if read(), read_us(), read_ms(),
kadonotakashi 0:8fdf9a60065b 412 * read_high_resolution_us()
kadonotakashi 0:8fdf9a60065b 413 * functions return time accumulated between
kadonotakashi 0:8fdf9a60065b 414 * timer starts and stops.
kadonotakashi 0:8fdf9a60065b 415 *
kadonotakashi 0:8fdf9a60065b 416 * Note this function assumes that Timer uses os ticker.
kadonotakashi 0:8fdf9a60065b 417 *
kadonotakashi 0:8fdf9a60065b 418 * Given Timer has been successfully created and
kadonotakashi 0:8fdf9a60065b 419 * few times started and stopped after a specified period of time.
kadonotakashi 0:8fdf9a60065b 420 * When timer read request is performed.
kadonotakashi 0:8fdf9a60065b 421 * Then read functions return accumulated time elapsed between starts
kadonotakashi 0:8fdf9a60065b 422 * and stops.
kadonotakashi 0:8fdf9a60065b 423 */
kadonotakashi 0:8fdf9a60065b 424 void test_timer_time_accumulation_os_ticker()
kadonotakashi 0:8fdf9a60065b 425 {
kadonotakashi 0:8fdf9a60065b 426 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 427 p_timer->start();
kadonotakashi 0:8fdf9a60065b 428
kadonotakashi 0:8fdf9a60065b 429 /* Wait 10 ms. */
kadonotakashi 0:8fdf9a60065b 430 busy_wait_ms(10);
kadonotakashi 0:8fdf9a60065b 431
kadonotakashi 0:8fdf9a60065b 432 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 433 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 434
kadonotakashi 0:8fdf9a60065b 435 /* Check results - totally 10 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 436 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 437 TEST_ASSERT_INT32_WITHIN(DELTA_MS(10), 10, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 438 TEST_ASSERT_INT32_WITHIN(DELTA_US(10), 10000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 439 TEST_ASSERT_UINT64_WITHIN(DELTA_US(10), 10000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 440
kadonotakashi 0:8fdf9a60065b 441 /* Wait 50 ms - this is done to show that time elapsed when
kadonotakashi 0:8fdf9a60065b 442 * the timer is stopped does not have influence on the
kadonotakashi 0:8fdf9a60065b 443 * timer counted time. */
kadonotakashi 0:8fdf9a60065b 444 busy_wait_ms(50);
kadonotakashi 0:8fdf9a60065b 445
kadonotakashi 0:8fdf9a60065b 446 /* ------ */
kadonotakashi 0:8fdf9a60065b 447
kadonotakashi 0:8fdf9a60065b 448 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 449 p_timer->start();
kadonotakashi 0:8fdf9a60065b 450
kadonotakashi 0:8fdf9a60065b 451 /* Wait 20 ms. */
kadonotakashi 0:8fdf9a60065b 452 busy_wait_ms(20);
kadonotakashi 0:8fdf9a60065b 453
kadonotakashi 0:8fdf9a60065b 454 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 455 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 456
kadonotakashi 0:8fdf9a60065b 457 /* Check results - totally 30 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 458 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(30), 0.030f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 459 TEST_ASSERT_INT32_WITHIN(DELTA_MS(30), 30, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 460 TEST_ASSERT_INT32_WITHIN(DELTA_US(30), 30000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 461 TEST_ASSERT_UINT64_WITHIN(DELTA_US(30), 30000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 462
kadonotakashi 0:8fdf9a60065b 463 /* Wait 50 ms - this is done to show that time elapsed when
kadonotakashi 0:8fdf9a60065b 464 * the timer is stopped does not have influence on the
kadonotakashi 0:8fdf9a60065b 465 * timer counted time. */
kadonotakashi 0:8fdf9a60065b 466
kadonotakashi 0:8fdf9a60065b 467 /* ------ */
kadonotakashi 0:8fdf9a60065b 468
kadonotakashi 0:8fdf9a60065b 469 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 470 p_timer->start();
kadonotakashi 0:8fdf9a60065b 471
kadonotakashi 0:8fdf9a60065b 472 /* Wait 30 ms. */
kadonotakashi 0:8fdf9a60065b 473 busy_wait_ms(30);
kadonotakashi 0:8fdf9a60065b 474
kadonotakashi 0:8fdf9a60065b 475 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 476 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 477
kadonotakashi 0:8fdf9a60065b 478 /* Check results - totally 60 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 479 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(60), 0.060f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 480 TEST_ASSERT_INT32_WITHIN(DELTA_MS(60), 60, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 481 TEST_ASSERT_INT32_WITHIN(DELTA_US(60), 60000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 482 TEST_ASSERT_UINT64_WITHIN(DELTA_US(60), 60000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 483
kadonotakashi 0:8fdf9a60065b 484 /* Wait 50 ms - this is done to show that time elapsed when
kadonotakashi 0:8fdf9a60065b 485 * the timer is stopped does not have influence on the
kadonotakashi 0:8fdf9a60065b 486 * timer time. */
kadonotakashi 0:8fdf9a60065b 487 busy_wait_ms(50);
kadonotakashi 0:8fdf9a60065b 488
kadonotakashi 0:8fdf9a60065b 489 /* ------ */
kadonotakashi 0:8fdf9a60065b 490
kadonotakashi 0:8fdf9a60065b 491 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 492 p_timer->start();
kadonotakashi 0:8fdf9a60065b 493
kadonotakashi 0:8fdf9a60065b 494 /* Wait 1 sec. */
kadonotakashi 0:8fdf9a60065b 495 busy_wait_ms(1000);
kadonotakashi 0:8fdf9a60065b 496
kadonotakashi 0:8fdf9a60065b 497 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 498 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 499
kadonotakashi 0:8fdf9a60065b 500 /* Check results - totally 1060 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 501 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1060), 1.060f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 502 TEST_ASSERT_INT32_WITHIN(DELTA_MS(1060), 1060, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 503 TEST_ASSERT_INT32_WITHIN(DELTA_US(1060), 1060000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 504 TEST_ASSERT_UINT64_WITHIN(DELTA_US(1060), 1060000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 505 }
kadonotakashi 0:8fdf9a60065b 506
kadonotakashi 0:8fdf9a60065b 507 /* This test verifies if reset() function resets the timer
kadonotakashi 0:8fdf9a60065b 508 * counted time.
kadonotakashi 0:8fdf9a60065b 509 *
kadonotakashi 0:8fdf9a60065b 510 * Note this function assumes that Timer uses os ticker.
kadonotakashi 0:8fdf9a60065b 511 *
kadonotakashi 0:8fdf9a60065b 512 * Given timer has been started and stopped once, then reset
kadonotakashi 0:8fdf9a60065b 513 * operation was performed.
kadonotakashi 0:8fdf9a60065b 514 * When timer is started and stopped next time.
kadonotakashi 0:8fdf9a60065b 515 * Then timer read functions returns only the the second
kadonotakashi 0:8fdf9a60065b 516 * measured time.
kadonotakashi 0:8fdf9a60065b 517 */
kadonotakashi 0:8fdf9a60065b 518 void test_timer_reset_os_ticker()
kadonotakashi 0:8fdf9a60065b 519 {
kadonotakashi 0:8fdf9a60065b 520 /* First measure 10 ms delay. */
kadonotakashi 0:8fdf9a60065b 521 p_timer->start();
kadonotakashi 0:8fdf9a60065b 522
kadonotakashi 0:8fdf9a60065b 523 /* Wait 10 ms. */
kadonotakashi 0:8fdf9a60065b 524 busy_wait_ms(10);
kadonotakashi 0:8fdf9a60065b 525
kadonotakashi 0:8fdf9a60065b 526 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 527 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 528
kadonotakashi 0:8fdf9a60065b 529 /* Check results - totally 10 ms elapsed. */
kadonotakashi 0:8fdf9a60065b 530 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 531 TEST_ASSERT_INT32_WITHIN(DELTA_MS(10), 10, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 532 TEST_ASSERT_INT32_WITHIN(DELTA_US(10), 10000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 533 TEST_ASSERT_UINT64_WITHIN(DELTA_US(10), 10000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 534
kadonotakashi 0:8fdf9a60065b 535 /* Reset the timer - previous measured time should be lost now. */
kadonotakashi 0:8fdf9a60065b 536 p_timer->reset();
kadonotakashi 0:8fdf9a60065b 537
kadonotakashi 0:8fdf9a60065b 538 /* Now measure 20 ms delay. */
kadonotakashi 0:8fdf9a60065b 539 p_timer->start();
kadonotakashi 0:8fdf9a60065b 540
kadonotakashi 0:8fdf9a60065b 541 /* Wait 20 ms. */
kadonotakashi 0:8fdf9a60065b 542 busy_wait_ms(20);
kadonotakashi 0:8fdf9a60065b 543
kadonotakashi 0:8fdf9a60065b 544 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 545 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 546
kadonotakashi 0:8fdf9a60065b 547 /* Check results - 20 ms elapsed since the reset. */
kadonotakashi 0:8fdf9a60065b 548 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(20), 0.020f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 549 TEST_ASSERT_INT32_WITHIN(DELTA_MS(20), 20, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 550 TEST_ASSERT_INT32_WITHIN(DELTA_US(20), 20000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 551 TEST_ASSERT_UINT64_WITHIN(DELTA_US(20), 20000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 552 }
kadonotakashi 0:8fdf9a60065b 553
kadonotakashi 0:8fdf9a60065b 554 /* This test verifies if reset() function resets the timer
kadonotakashi 0:8fdf9a60065b 555 * counted time.
kadonotakashi 0:8fdf9a60065b 556 *
kadonotakashi 0:8fdf9a60065b 557 * Note this function assumes that Timer uses user ticker.
kadonotakashi 0:8fdf9a60065b 558 *
kadonotakashi 0:8fdf9a60065b 559 * Given timer has been started and stopped once, then reset
kadonotakashi 0:8fdf9a60065b 560 * operation was performed.
kadonotakashi 0:8fdf9a60065b 561 * When timer is started and stopped next time.
kadonotakashi 0:8fdf9a60065b 562 * Then timer read functions returns only the the second
kadonotakashi 0:8fdf9a60065b 563 * measured time.
kadonotakashi 0:8fdf9a60065b 564 */
kadonotakashi 0:8fdf9a60065b 565 void test_timer_reset_user_ticker()
kadonotakashi 0:8fdf9a60065b 566 {
kadonotakashi 0:8fdf9a60065b 567 /* For timer which is using user ticker simulate set current
kadonotakashi 0:8fdf9a60065b 568 * time (irrelevant in case of os ticker). */
kadonotakashi 0:8fdf9a60065b 569 curr_ticker_ticks_val = 0;
kadonotakashi 0:8fdf9a60065b 570
kadonotakashi 0:8fdf9a60065b 571 /* First measure 10 ms delay. */
kadonotakashi 0:8fdf9a60065b 572 p_timer->start();
kadonotakashi 0:8fdf9a60065b 573
kadonotakashi 0:8fdf9a60065b 574 /* Simulate that 10 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 575 curr_ticker_ticks_val = 10000;
kadonotakashi 0:8fdf9a60065b 576
kadonotakashi 0:8fdf9a60065b 577 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 578 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 579
kadonotakashi 0:8fdf9a60065b 580 /* Check results - totally 10 ms elapsed. */
kadonotakashi 0:8fdf9a60065b 581 TEST_ASSERT_EQUAL_FLOAT(0.010f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 582 TEST_ASSERT_EQUAL_INT32(10, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 583 TEST_ASSERT_EQUAL_INT32(10000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 584 TEST_ASSERT_EQUAL_UINT64(10000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 585
kadonotakashi 0:8fdf9a60065b 586 /* Reset the timer - previous measured time should be lost now. */
kadonotakashi 0:8fdf9a60065b 587 p_timer->reset();
kadonotakashi 0:8fdf9a60065b 588
kadonotakashi 0:8fdf9a60065b 589 /* Now measure 20 ms delay. */
kadonotakashi 0:8fdf9a60065b 590 p_timer->start();
kadonotakashi 0:8fdf9a60065b 591
kadonotakashi 0:8fdf9a60065b 592 /* Simulate that 20 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 593 curr_ticker_ticks_val = 30000;
kadonotakashi 0:8fdf9a60065b 594
kadonotakashi 0:8fdf9a60065b 595 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 596 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 597
kadonotakashi 0:8fdf9a60065b 598 /* Check results - 20 ms elapsed since the reset. */
kadonotakashi 0:8fdf9a60065b 599 TEST_ASSERT_EQUAL_FLOAT(0.020f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 600 TEST_ASSERT_EQUAL_INT32(20, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 601 TEST_ASSERT_EQUAL_INT32(20000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 602 TEST_ASSERT_EQUAL_UINT64(20000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 603 }
kadonotakashi 0:8fdf9a60065b 604
kadonotakashi 0:8fdf9a60065b 605 /* This test verifies if calling start() for already
kadonotakashi 0:8fdf9a60065b 606 * started timer does nothing.
kadonotakashi 0:8fdf9a60065b 607 *
kadonotakashi 0:8fdf9a60065b 608 * Note this function assumes that Timer uses os ticker.
kadonotakashi 0:8fdf9a60065b 609 *
kadonotakashi 0:8fdf9a60065b 610 * Given timer is already started.
kadonotakashi 0:8fdf9a60065b 611 * When timer is started again.
kadonotakashi 0:8fdf9a60065b 612 * Then second start operation is ignored.
kadonotakashi 0:8fdf9a60065b 613 */
kadonotakashi 0:8fdf9a60065b 614 void test_timer_start_started_timer_os_ticker()
kadonotakashi 0:8fdf9a60065b 615 {
kadonotakashi 0:8fdf9a60065b 616 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 617 p_timer->start();
kadonotakashi 0:8fdf9a60065b 618
kadonotakashi 0:8fdf9a60065b 619 /* Wait 10 ms. */
kadonotakashi 0:8fdf9a60065b 620 busy_wait_ms(10);
kadonotakashi 0:8fdf9a60065b 621
kadonotakashi 0:8fdf9a60065b 622 /* Now start timer again. */
kadonotakashi 0:8fdf9a60065b 623 p_timer->start();
kadonotakashi 0:8fdf9a60065b 624
kadonotakashi 0:8fdf9a60065b 625 /* Wait 20 ms. */
kadonotakashi 0:8fdf9a60065b 626 busy_wait_ms(20);
kadonotakashi 0:8fdf9a60065b 627
kadonotakashi 0:8fdf9a60065b 628 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 629 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 630
kadonotakashi 0:8fdf9a60065b 631 /* Check results - 30 ms have elapsed since the first start. */
kadonotakashi 0:8fdf9a60065b 632 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(30), 0.030f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 633 TEST_ASSERT_INT32_WITHIN(DELTA_MS(30), 30, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 634 TEST_ASSERT_INT32_WITHIN(DELTA_US(30), 30000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 635 TEST_ASSERT_UINT64_WITHIN(DELTA_US(30), 30000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 636 }
kadonotakashi 0:8fdf9a60065b 637
kadonotakashi 0:8fdf9a60065b 638 /* This test verifies if calling start() for already
kadonotakashi 0:8fdf9a60065b 639 * started timer does nothing.
kadonotakashi 0:8fdf9a60065b 640 *
kadonotakashi 0:8fdf9a60065b 641 * Note this function assumes that Timer uses user ticker.
kadonotakashi 0:8fdf9a60065b 642 *
kadonotakashi 0:8fdf9a60065b 643 * Given timer is already started.
kadonotakashi 0:8fdf9a60065b 644 * When timer is started again.
kadonotakashi 0:8fdf9a60065b 645 * Then second start operation is ignored.
kadonotakashi 0:8fdf9a60065b 646 */
kadonotakashi 0:8fdf9a60065b 647 void test_timer_start_started_timer_user_ticker()
kadonotakashi 0:8fdf9a60065b 648 {
kadonotakashi 0:8fdf9a60065b 649 /* For timer which is using user ticker set current
kadonotakashi 0:8fdf9a60065b 650 * time (irrelevant in case of os ticker). */
kadonotakashi 0:8fdf9a60065b 651 curr_ticker_ticks_val = 0;
kadonotakashi 0:8fdf9a60065b 652
kadonotakashi 0:8fdf9a60065b 653 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 654 p_timer->start();
kadonotakashi 0:8fdf9a60065b 655
kadonotakashi 0:8fdf9a60065b 656 /* Simulate that 10 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 657 curr_ticker_ticks_val = 10000;
kadonotakashi 0:8fdf9a60065b 658
kadonotakashi 0:8fdf9a60065b 659 /* Now start timer again. */
kadonotakashi 0:8fdf9a60065b 660 p_timer->start();
kadonotakashi 0:8fdf9a60065b 661
kadonotakashi 0:8fdf9a60065b 662 /* Simulate that 20 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 663 curr_ticker_ticks_val = 30000;
kadonotakashi 0:8fdf9a60065b 664
kadonotakashi 0:8fdf9a60065b 665 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 666 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 667
kadonotakashi 0:8fdf9a60065b 668 /* Check results - 30 ms have elapsed since the first start. */
kadonotakashi 0:8fdf9a60065b 669 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(30), 0.030f, p_timer->read());
kadonotakashi 0:8fdf9a60065b 670 TEST_ASSERT_INT32_WITHIN(DELTA_MS(30), 30, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 671 TEST_ASSERT_INT32_WITHIN(DELTA_US(30), 30000, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 672 TEST_ASSERT_UINT64_WITHIN(DELTA_US(30), 30000, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 673 }
kadonotakashi 0:8fdf9a60065b 674
kadonotakashi 0:8fdf9a60065b 675 /* This test verifies Timer float operator.
kadonotakashi 0:8fdf9a60065b 676 *
kadonotakashi 0:8fdf9a60065b 677 * Note this function assumes that Timer uses os ticker.
kadonotakashi 0:8fdf9a60065b 678 *
kadonotakashi 0:8fdf9a60065b 679 * Given timer is created and a time period time is counted.
kadonotakashi 0:8fdf9a60065b 680 * When timer object is casted on float type.
kadonotakashi 0:8fdf9a60065b 681 * Then counted type in seconds is returned by means of
kadonotakashi 0:8fdf9a60065b 682 * read() function.
kadonotakashi 0:8fdf9a60065b 683 */
kadonotakashi 0:8fdf9a60065b 684 void test_timer_float_operator_os_ticker()
kadonotakashi 0:8fdf9a60065b 685 {
kadonotakashi 0:8fdf9a60065b 686 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 687 p_timer->start();
kadonotakashi 0:8fdf9a60065b 688
kadonotakashi 0:8fdf9a60065b 689 /* Wait 10 ms. */
kadonotakashi 0:8fdf9a60065b 690 busy_wait_ms(10);
kadonotakashi 0:8fdf9a60065b 691
kadonotakashi 0:8fdf9a60065b 692 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 693 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 694
kadonotakashi 0:8fdf9a60065b 695 /* Check result - 10 ms elapsed. */
kadonotakashi 0:8fdf9a60065b 696 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, (float)(*p_timer));
kadonotakashi 0:8fdf9a60065b 697 }
kadonotakashi 0:8fdf9a60065b 698
kadonotakashi 0:8fdf9a60065b 699 /* This test verifies Timer float operator.
kadonotakashi 0:8fdf9a60065b 700 *
kadonotakashi 0:8fdf9a60065b 701 * Note this function assumes that Timer uses user ticker.
kadonotakashi 0:8fdf9a60065b 702 *
kadonotakashi 0:8fdf9a60065b 703 * Given timer is created and a time period time is counted.
kadonotakashi 0:8fdf9a60065b 704 * When timer object is casted on float type.
kadonotakashi 0:8fdf9a60065b 705 * Then counted type in seconds is returned by means of
kadonotakashi 0:8fdf9a60065b 706 * read() function.
kadonotakashi 0:8fdf9a60065b 707 */
kadonotakashi 0:8fdf9a60065b 708 void test_timer_float_operator_user_ticker()
kadonotakashi 0:8fdf9a60065b 709 {
kadonotakashi 0:8fdf9a60065b 710 /* For timer which is using user ticker set current
kadonotakashi 0:8fdf9a60065b 711 * time (irrelevant in case of os ticker). */
kadonotakashi 0:8fdf9a60065b 712 curr_ticker_ticks_val = 0;
kadonotakashi 0:8fdf9a60065b 713
kadonotakashi 0:8fdf9a60065b 714 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 715 p_timer->start();
kadonotakashi 0:8fdf9a60065b 716
kadonotakashi 0:8fdf9a60065b 717 /* Simulate that 10 ms have elapsed. */
kadonotakashi 0:8fdf9a60065b 718 curr_ticker_ticks_val = 10000;
kadonotakashi 0:8fdf9a60065b 719
kadonotakashi 0:8fdf9a60065b 720 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 721 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 722
kadonotakashi 0:8fdf9a60065b 723 /* Check result - 10 ms elapsed. */
kadonotakashi 0:8fdf9a60065b 724 TEST_ASSERT_EQUAL_FLOAT(0.010f, (float)(*p_timer));
kadonotakashi 0:8fdf9a60065b 725 }
kadonotakashi 0:8fdf9a60065b 726
kadonotakashi 0:8fdf9a60065b 727 /* This test verifies if time counted by the timer is
kadonotakashi 0:8fdf9a60065b 728 * valid.
kadonotakashi 0:8fdf9a60065b 729 *
kadonotakashi 0:8fdf9a60065b 730 * For this test Timer which uses os ticker
kadonotakashi 0:8fdf9a60065b 731 * must be used.
kadonotakashi 0:8fdf9a60065b 732 *
kadonotakashi 0:8fdf9a60065b 733 * Given timer is created.
kadonotakashi 0:8fdf9a60065b 734 * When timer is used to measure 1ms/10ms/100ms/1s
kadonotakashi 0:8fdf9a60065b 735 * delays.
kadonotakashi 0:8fdf9a60065b 736 * Then the results are valid (within acceptable range).
kadonotakashi 0:8fdf9a60065b 737 */
kadonotakashi 0:8fdf9a60065b 738 template<int wait_val_us>
kadonotakashi 0:8fdf9a60065b 739 void test_timer_time_measurement()
kadonotakashi 0:8fdf9a60065b 740 {
kadonotakashi 0:8fdf9a60065b 741 /* Start the timer. */
kadonotakashi 0:8fdf9a60065b 742 p_timer->start();
kadonotakashi 0:8fdf9a60065b 743
kadonotakashi 0:8fdf9a60065b 744 /* Wait <wait_val_us> us. */
kadonotakashi 0:8fdf9a60065b 745 busy_wait_us(wait_val_us);
kadonotakashi 0:8fdf9a60065b 746
kadonotakashi 0:8fdf9a60065b 747 /* Stop the timer. */
kadonotakashi 0:8fdf9a60065b 748 p_timer->stop();
kadonotakashi 0:8fdf9a60065b 749
kadonotakashi 0:8fdf9a60065b 750 /* Check results. */
kadonotakashi 0:8fdf9a60065b 751 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(wait_val_us / US_PER_MSEC), (float)wait_val_us / US_PER_SEC, p_timer->read());
kadonotakashi 0:8fdf9a60065b 752 TEST_ASSERT_INT32_WITHIN(DELTA_MS(wait_val_us / US_PER_MSEC), wait_val_us / US_PER_MSEC, p_timer->read_ms());
kadonotakashi 0:8fdf9a60065b 753 TEST_ASSERT_INT32_WITHIN(DELTA_US(wait_val_us / US_PER_MSEC), wait_val_us, p_timer->read_us());
kadonotakashi 0:8fdf9a60065b 754 TEST_ASSERT_UINT64_WITHIN(DELTA_US(wait_val_us / US_PER_MSEC), wait_val_us, p_timer->read_high_resolution_us());
kadonotakashi 0:8fdf9a60065b 755 }
kadonotakashi 0:8fdf9a60065b 756
kadonotakashi 0:8fdf9a60065b 757 utest::v1::status_t test_setup(const size_t number_of_cases)
kadonotakashi 0:8fdf9a60065b 758 {
kadonotakashi 0:8fdf9a60065b 759 GREENTEA_SETUP(15, "default_auto");
kadonotakashi 0:8fdf9a60065b 760 return verbose_test_setup_handler(number_of_cases);
kadonotakashi 0:8fdf9a60065b 761 }
kadonotakashi 0:8fdf9a60065b 762
kadonotakashi 0:8fdf9a60065b 763 Case cases[] = {
kadonotakashi 0:8fdf9a60065b 764 Case("Test: Timer (based on os ticker) is stopped after creation.", timer_os_ticker_setup_handler, test_timer_creation_os_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 765 Case("Test: Timer (based on user ticker) is stopped after creation.", timer_user_ticker_setup_handler, test_timer_creation_user_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 766
kadonotakashi 0:8fdf9a60065b 767 Case("Test: Timer (based on os ticker) - measured time accumulation.", timer_os_ticker_setup_handler, test_timer_time_accumulation_os_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 768 Case("Test: Timer (based on user ticker) measured time accumulation.", timer_user_ticker_setup_handler, test_timer_time_accumulation_user_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 769
kadonotakashi 0:8fdf9a60065b 770 Case("Test: Timer (based on os ticker) - reset.", timer_os_ticker_setup_handler, test_timer_reset_os_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 771 Case("Test: Timer (based on user ticker) - reset.", timer_user_ticker_setup_handler, test_timer_reset_user_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 772
kadonotakashi 0:8fdf9a60065b 773 Case("Test: Timer (based on os ticker) - start started timer.", timer_os_ticker_setup_handler, test_timer_start_started_timer_os_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 774 Case("Test: Timer (based on user ticker) - start started timer.", timer_user_ticker_setup_handler, test_timer_start_started_timer_user_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 775
kadonotakashi 0:8fdf9a60065b 776 Case("Test: Timer (based on os ticker) - float operator.", timer_os_ticker_setup_handler, test_timer_float_operator_os_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 777 Case("Test: Timer (based on user ticker) - float operator.", timer_user_ticker_setup_handler, test_timer_float_operator_user_ticker, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 778
kadonotakashi 0:8fdf9a60065b 779 Case("Test: Timer - time measurement 1 ms.", timer_os_ticker_setup_handler, test_timer_time_measurement<1000>, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 780 Case("Test: Timer - time measurement 10 ms.", timer_os_ticker_setup_handler, test_timer_time_measurement<10000>, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 781 Case("Test: Timer - time measurement 100 ms.", timer_os_ticker_setup_handler, test_timer_time_measurement<100000>, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 782 Case("Test: Timer - time measurement 1 s.", timer_os_ticker_setup_handler, test_timer_time_measurement<1000000>, cleanup_handler),
kadonotakashi 0:8fdf9a60065b 783 };
kadonotakashi 0:8fdf9a60065b 784
kadonotakashi 0:8fdf9a60065b 785 Specification specification(test_setup, cases);
kadonotakashi 0:8fdf9a60065b 786
kadonotakashi 0:8fdf9a60065b 787 int main()
kadonotakashi 0:8fdf9a60065b 788 {
kadonotakashi 0:8fdf9a60065b 789 return !Harness::run(specification);
kadonotakashi 0:8fdf9a60065b 790 }
kadonotakashi 0:8fdf9a60065b 791