![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbed_hal/common_tickers_freq/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 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2013-2016, 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 | /* |
kadonotakashi | 0:8fdf9a60065b | 19 | * This is the mbed device part of the test to verify if mbed board ticker |
kadonotakashi | 0:8fdf9a60065b | 20 | * freqency is valid. |
kadonotakashi | 0:8fdf9a60065b | 21 | */ |
kadonotakashi | 0:8fdf9a60065b | 22 | |
kadonotakashi | 0:8fdf9a60065b | 23 | |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 26 | #include "utest/utest.h" |
kadonotakashi | 0:8fdf9a60065b | 27 | #include "unity/unity.h" |
kadonotakashi | 0:8fdf9a60065b | 28 | #include "ticker_api_test_freq.h" |
kadonotakashi | 0:8fdf9a60065b | 29 | #include "hal/us_ticker_api.h" |
kadonotakashi | 0:8fdf9a60065b | 30 | #include "hal/lp_ticker_api.h" |
kadonotakashi | 0:8fdf9a60065b | 31 | #include "hal/mbed_lp_ticker_wrapper.h" |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | #if !DEVICE_USTICKER |
kadonotakashi | 0:8fdf9a60065b | 34 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 35 | #endif |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | //FastModels not support time drifting test |
kadonotakashi | 0:8fdf9a60065b | 38 | #if defined(__ARM_FM) |
kadonotakashi | 0:8fdf9a60065b | 39 | #error [NOT_SUPPORTED] test not supported |
kadonotakashi | 0:8fdf9a60065b | 40 | #endif |
kadonotakashi | 0:8fdf9a60065b | 41 | |
kadonotakashi | 0:8fdf9a60065b | 42 | #define US_PER_S 1000000 |
kadonotakashi | 0:8fdf9a60065b | 43 | |
kadonotakashi | 0:8fdf9a60065b | 44 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 45 | |
kadonotakashi | 0:8fdf9a60065b | 46 | const ticker_interface_t *intf; |
kadonotakashi | 0:8fdf9a60065b | 47 | uint32_t intf_mask; |
kadonotakashi | 0:8fdf9a60065b | 48 | uint32_t intf_last_tick; |
kadonotakashi | 0:8fdf9a60065b | 49 | uint32_t intf_elapsed_ticks; |
kadonotakashi | 0:8fdf9a60065b | 50 | ticker_irq_handler_type prev_handler; |
kadonotakashi | 0:8fdf9a60065b | 51 | |
kadonotakashi | 0:8fdf9a60065b | 52 | uint32_t ticks_to_us(uint32_t ticks, uint32_t freq) |
kadonotakashi | 0:8fdf9a60065b | 53 | { |
kadonotakashi | 0:8fdf9a60065b | 54 | return (uint32_t)((uint64_t)ticks * US_PER_S / freq); |
kadonotakashi | 0:8fdf9a60065b | 55 | } |
kadonotakashi | 0:8fdf9a60065b | 56 | |
kadonotakashi | 0:8fdf9a60065b | 57 | void elapsed_ticks_reset() |
kadonotakashi | 0:8fdf9a60065b | 58 | { |
kadonotakashi | 0:8fdf9a60065b | 59 | core_util_critical_section_enter(); |
kadonotakashi | 0:8fdf9a60065b | 60 | |
kadonotakashi | 0:8fdf9a60065b | 61 | const uint32_t ticker_bits = intf->get_info()->bits; |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | intf_mask = (1 << ticker_bits) - 1; |
kadonotakashi | 0:8fdf9a60065b | 64 | intf_last_tick = intf->read(); |
kadonotakashi | 0:8fdf9a60065b | 65 | intf_elapsed_ticks = 0; |
kadonotakashi | 0:8fdf9a60065b | 66 | |
kadonotakashi | 0:8fdf9a60065b | 67 | core_util_critical_section_exit(); |
kadonotakashi | 0:8fdf9a60065b | 68 | } |
kadonotakashi | 0:8fdf9a60065b | 69 | |
kadonotakashi | 0:8fdf9a60065b | 70 | uint32_t elapsed_ticks_update() |
kadonotakashi | 0:8fdf9a60065b | 71 | { |
kadonotakashi | 0:8fdf9a60065b | 72 | core_util_critical_section_enter(); |
kadonotakashi | 0:8fdf9a60065b | 73 | |
kadonotakashi | 0:8fdf9a60065b | 74 | const uint32_t current_tick = intf->read(); |
kadonotakashi | 0:8fdf9a60065b | 75 | intf_elapsed_ticks += (current_tick - intf_last_tick) & intf_mask; |
kadonotakashi | 0:8fdf9a60065b | 76 | intf_last_tick = current_tick; |
kadonotakashi | 0:8fdf9a60065b | 77 | |
kadonotakashi | 0:8fdf9a60065b | 78 | /* Schedule next interrupt half way to overflow */ |
kadonotakashi | 0:8fdf9a60065b | 79 | uint32_t next = (current_tick + intf_mask / 2) & intf_mask; |
kadonotakashi | 0:8fdf9a60065b | 80 | intf->set_interrupt(next); |
kadonotakashi | 0:8fdf9a60065b | 81 | |
kadonotakashi | 0:8fdf9a60065b | 82 | uint32_t elapsed = intf_elapsed_ticks; |
kadonotakashi | 0:8fdf9a60065b | 83 | |
kadonotakashi | 0:8fdf9a60065b | 84 | core_util_critical_section_exit(); |
kadonotakashi | 0:8fdf9a60065b | 85 | return elapsed; |
kadonotakashi | 0:8fdf9a60065b | 86 | } |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | void ticker_event_handler_stub(const ticker_data_t *const ticker) |
kadonotakashi | 0:8fdf9a60065b | 89 | { |
kadonotakashi | 0:8fdf9a60065b | 90 | intf->clear_interrupt(); |
kadonotakashi | 0:8fdf9a60065b | 91 | elapsed_ticks_update(); |
kadonotakashi | 0:8fdf9a60065b | 92 | } |
kadonotakashi | 0:8fdf9a60065b | 93 | |
kadonotakashi | 0:8fdf9a60065b | 94 | /* Test that the ticker is operating at the frequency it specifies. */ |
kadonotakashi | 0:8fdf9a60065b | 95 | void ticker_frequency_test() |
kadonotakashi | 0:8fdf9a60065b | 96 | { |
kadonotakashi | 0:8fdf9a60065b | 97 | char _key[11] = { }; |
kadonotakashi | 0:8fdf9a60065b | 98 | char _value[128] = { }; |
kadonotakashi | 0:8fdf9a60065b | 99 | int expected_key = 1; |
kadonotakashi | 0:8fdf9a60065b | 100 | const uint32_t ticker_freq = intf->get_info()->frequency; |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | intf->init(); |
kadonotakashi | 0:8fdf9a60065b | 103 | |
kadonotakashi | 0:8fdf9a60065b | 104 | elapsed_ticks_reset(); |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | /* Detect overflow for tickers with lower counters width. */ |
kadonotakashi | 0:8fdf9a60065b | 107 | elapsed_ticks_update(); |
kadonotakashi | 0:8fdf9a60065b | 108 | |
kadonotakashi | 0:8fdf9a60065b | 109 | greentea_send_kv("timing_drift_check_start", 0); |
kadonotakashi | 0:8fdf9a60065b | 110 | |
kadonotakashi | 0:8fdf9a60065b | 111 | /* Wait for 1st signal from host. */ |
kadonotakashi | 0:8fdf9a60065b | 112 | do { |
kadonotakashi | 0:8fdf9a60065b | 113 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
kadonotakashi | 0:8fdf9a60065b | 114 | expected_key = strcmp(_key, "base_time"); |
kadonotakashi | 0:8fdf9a60065b | 115 | } while (expected_key); |
kadonotakashi | 0:8fdf9a60065b | 116 | |
kadonotakashi | 0:8fdf9a60065b | 117 | const uint32_t begin_ticks = elapsed_ticks_update(); |
kadonotakashi | 0:8fdf9a60065b | 118 | |
kadonotakashi | 0:8fdf9a60065b | 119 | /* Assume that there was no overflow at this point - we are just after init. */ |
kadonotakashi | 0:8fdf9a60065b | 120 | greentea_send_kv(_key, ticks_to_us(begin_ticks, ticker_freq)); |
kadonotakashi | 0:8fdf9a60065b | 121 | |
kadonotakashi | 0:8fdf9a60065b | 122 | /* Wait for 2nd signal from host. */ |
kadonotakashi | 0:8fdf9a60065b | 123 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | const uint32_t end_ticks = elapsed_ticks_update(); |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | greentea_send_kv(_key, ticks_to_us(end_ticks, ticker_freq)); |
kadonotakashi | 0:8fdf9a60065b | 128 | |
kadonotakashi | 0:8fdf9a60065b | 129 | /* Get the results from host. */ |
kadonotakashi | 0:8fdf9a60065b | 130 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
kadonotakashi | 0:8fdf9a60065b | 131 | |
kadonotakashi | 0:8fdf9a60065b | 132 | TEST_ASSERT_EQUAL_STRING_MESSAGE("pass", _key, "Host side script reported a fail..."); |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | intf->disable_interrupt(); |
kadonotakashi | 0:8fdf9a60065b | 135 | } |
kadonotakashi | 0:8fdf9a60065b | 136 | |
kadonotakashi | 0:8fdf9a60065b | 137 | utest::v1::status_t us_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) |
kadonotakashi | 0:8fdf9a60065b | 138 | { |
kadonotakashi | 0:8fdf9a60065b | 139 | #if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0) |
kadonotakashi | 0:8fdf9a60065b | 140 | /* Suspend the lp ticker wrapper since it makes use of the us ticker */ |
kadonotakashi | 0:8fdf9a60065b | 141 | ticker_suspend(get_lp_ticker_data()); |
kadonotakashi | 0:8fdf9a60065b | 142 | lp_ticker_wrapper_suspend(); |
kadonotakashi | 0:8fdf9a60065b | 143 | #endif |
kadonotakashi | 0:8fdf9a60065b | 144 | ticker_suspend(get_us_ticker_data()); |
kadonotakashi | 0:8fdf9a60065b | 145 | intf = get_us_ticker_data()->interface; |
kadonotakashi | 0:8fdf9a60065b | 146 | prev_handler = set_us_ticker_irq_handler(ticker_event_handler_stub); |
kadonotakashi | 0:8fdf9a60065b | 147 | return greentea_case_setup_handler(source, index_of_case); |
kadonotakashi | 0:8fdf9a60065b | 148 | } |
kadonotakashi | 0:8fdf9a60065b | 149 | |
kadonotakashi | 0:8fdf9a60065b | 150 | utest::v1::status_t us_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed, |
kadonotakashi | 0:8fdf9a60065b | 151 | const failure_t reason) |
kadonotakashi | 0:8fdf9a60065b | 152 | { |
kadonotakashi | 0:8fdf9a60065b | 153 | set_us_ticker_irq_handler(prev_handler); |
kadonotakashi | 0:8fdf9a60065b | 154 | ticker_resume(get_us_ticker_data()); |
kadonotakashi | 0:8fdf9a60065b | 155 | #if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0) |
kadonotakashi | 0:8fdf9a60065b | 156 | lp_ticker_wrapper_resume(); |
kadonotakashi | 0:8fdf9a60065b | 157 | ticker_resume(get_lp_ticker_data()); |
kadonotakashi | 0:8fdf9a60065b | 158 | #endif |
kadonotakashi | 0:8fdf9a60065b | 159 | return greentea_case_teardown_handler(source, passed, failed, reason); |
kadonotakashi | 0:8fdf9a60065b | 160 | } |
kadonotakashi | 0:8fdf9a60065b | 161 | |
kadonotakashi | 0:8fdf9a60065b | 162 | #if DEVICE_LPTICKER |
kadonotakashi | 0:8fdf9a60065b | 163 | utest::v1::status_t lp_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) |
kadonotakashi | 0:8fdf9a60065b | 164 | { |
kadonotakashi | 0:8fdf9a60065b | 165 | ticker_suspend(get_lp_ticker_data()); |
kadonotakashi | 0:8fdf9a60065b | 166 | intf = get_lp_ticker_data()->interface; |
kadonotakashi | 0:8fdf9a60065b | 167 | prev_handler = set_lp_ticker_irq_handler(ticker_event_handler_stub); |
kadonotakashi | 0:8fdf9a60065b | 168 | return greentea_case_setup_handler(source, index_of_case); |
kadonotakashi | 0:8fdf9a60065b | 169 | } |
kadonotakashi | 0:8fdf9a60065b | 170 | |
kadonotakashi | 0:8fdf9a60065b | 171 | utest::v1::status_t lp_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed, |
kadonotakashi | 0:8fdf9a60065b | 172 | const failure_t reason) |
kadonotakashi | 0:8fdf9a60065b | 173 | { |
kadonotakashi | 0:8fdf9a60065b | 174 | set_lp_ticker_irq_handler(prev_handler); |
kadonotakashi | 0:8fdf9a60065b | 175 | ticker_resume(get_lp_ticker_data()); |
kadonotakashi | 0:8fdf9a60065b | 176 | return greentea_case_teardown_handler(source, passed, failed, reason); |
kadonotakashi | 0:8fdf9a60065b | 177 | } |
kadonotakashi | 0:8fdf9a60065b | 178 | #endif |
kadonotakashi | 0:8fdf9a60065b | 179 | |
kadonotakashi | 0:8fdf9a60065b | 180 | // Test cases |
kadonotakashi | 0:8fdf9a60065b | 181 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 182 | Case("Microsecond ticker frequency test", us_ticker_case_setup_handler_t, ticker_frequency_test, |
kadonotakashi | 0:8fdf9a60065b | 183 | us_ticker_case_teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 184 | #if DEVICE_LPTICKER |
kadonotakashi | 0:8fdf9a60065b | 185 | Case("Low power ticker frequency test", lp_ticker_case_setup_handler_t, ticker_frequency_test, |
kadonotakashi | 0:8fdf9a60065b | 186 | lp_ticker_case_teardown_handler_t), |
kadonotakashi | 0:8fdf9a60065b | 187 | #endif |
kadonotakashi | 0:8fdf9a60065b | 188 | }; |
kadonotakashi | 0:8fdf9a60065b | 189 | |
kadonotakashi | 0:8fdf9a60065b | 190 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 191 | { |
kadonotakashi | 0:8fdf9a60065b | 192 | /* Suspend RTOS Kernel so the timers are not in use. */ |
kadonotakashi | 0:8fdf9a60065b | 193 | osKernelSuspend(); |
kadonotakashi | 0:8fdf9a60065b | 194 | |
kadonotakashi | 0:8fdf9a60065b | 195 | GREENTEA_SETUP(120, "timing_drift_auto"); |
kadonotakashi | 0:8fdf9a60065b | 196 | return greentea_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 197 | } |
kadonotakashi | 0:8fdf9a60065b | 198 | |
kadonotakashi | 0:8fdf9a60065b | 199 | void greentea_test_teardown(const size_t passed, const size_t failed, const failure_t failure) |
kadonotakashi | 0:8fdf9a60065b | 200 | { |
kadonotakashi | 0:8fdf9a60065b | 201 | osKernelResume(0); |
kadonotakashi | 0:8fdf9a60065b | 202 | |
kadonotakashi | 0:8fdf9a60065b | 203 | greentea_test_teardown_handler(passed, failed, failure); |
kadonotakashi | 0:8fdf9a60065b | 204 | } |
kadonotakashi | 0:8fdf9a60065b | 205 | |
kadonotakashi | 0:8fdf9a60065b | 206 | Specification specification(greentea_test_setup, cases, greentea_test_teardown); |
kadonotakashi | 0:8fdf9a60065b | 207 | |
kadonotakashi | 0:8fdf9a60065b | 208 | int main() |
kadonotakashi | 0:8fdf9a60065b | 209 | { |
kadonotakashi | 0:8fdf9a60065b | 210 | Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 211 | } |