takashi kadono
/
Nucleo446_SSD1331
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbed_hal/rtc/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 | |
kadonotakashi | 0:8fdf9a60065b | 17 | #if !DEVICE_RTC |
kadonotakashi | 0:8fdf9a60065b | 18 | #error [NOT_SUPPORTED] RTC API not supported for this target |
kadonotakashi | 0:8fdf9a60065b | 19 | #endif |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "utest/utest.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "unity/unity.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "rtc_test.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 27 | #include "rtc_api.h" |
kadonotakashi | 0:8fdf9a60065b | 28 | |
kadonotakashi | 0:8fdf9a60065b | 29 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | static const uint32_t WAIT_TIME = 4; |
kadonotakashi | 0:8fdf9a60065b | 32 | static const uint32_t WAIT_TOLERANCE = 1; |
kadonotakashi | 0:8fdf9a60065b | 33 | |
kadonotakashi | 0:8fdf9a60065b | 34 | #define US_PER_SEC 1000000 |
kadonotakashi | 0:8fdf9a60065b | 35 | #define ACCURACY_FACTOR 10 |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | static const uint32_t DELAY_4S = 4; |
kadonotakashi | 0:8fdf9a60065b | 38 | static const uint32_t DELAY_10S = 10; |
kadonotakashi | 0:8fdf9a60065b | 39 | static const uint32_t RTC_TOLERANCE = 1; |
kadonotakashi | 0:8fdf9a60065b | 40 | static const uint32_t TOLERANCE_ACCURACY_US = (DELAY_10S *US_PER_SEC / ACCURACY_FACTOR); |
kadonotakashi | 0:8fdf9a60065b | 41 | |
kadonotakashi | 0:8fdf9a60065b | 42 | #if DEVICE_LPTICKER |
kadonotakashi | 0:8fdf9a60065b | 43 | volatile bool expired; |
kadonotakashi | 0:8fdf9a60065b | 44 | |
kadonotakashi | 0:8fdf9a60065b | 45 | void callback(void) |
kadonotakashi | 0:8fdf9a60065b | 46 | { |
kadonotakashi | 0:8fdf9a60065b | 47 | expired = true; |
kadonotakashi | 0:8fdf9a60065b | 48 | } |
kadonotakashi | 0:8fdf9a60065b | 49 | |
kadonotakashi | 0:8fdf9a60065b | 50 | /* Auxiliary function to test if RTC continue counting in |
kadonotakashi | 0:8fdf9a60065b | 51 | * sleep and deep-sleep modes. */ |
kadonotakashi | 0:8fdf9a60065b | 52 | void rtc_sleep_test_support(bool deepsleep_mode) |
kadonotakashi | 0:8fdf9a60065b | 53 | { |
kadonotakashi | 0:8fdf9a60065b | 54 | LowPowerTimeout timeout; |
kadonotakashi | 0:8fdf9a60065b | 55 | const uint32_t start = 100; |
kadonotakashi | 0:8fdf9a60065b | 56 | expired = false; |
kadonotakashi | 0:8fdf9a60065b | 57 | |
kadonotakashi | 0:8fdf9a60065b | 58 | /* |
kadonotakashi | 0:8fdf9a60065b | 59 | * Since deepsleep() may shut down the UART peripheral, we wait for 10ms |
kadonotakashi | 0:8fdf9a60065b | 60 | * to allow for hardware serial buffers to completely flush. |
kadonotakashi | 0:8fdf9a60065b | 61 | * This should be replaced with a better function that checks if the |
kadonotakashi | 0:8fdf9a60065b | 62 | * hardware buffers are empty. However, such an API does not exist now, |
kadonotakashi | 0:8fdf9a60065b | 63 | * so we'll use the wait_ms() function for now. |
kadonotakashi | 0:8fdf9a60065b | 64 | */ |
kadonotakashi | 0:8fdf9a60065b | 65 | wait_ms(10); |
kadonotakashi | 0:8fdf9a60065b | 66 | |
kadonotakashi | 0:8fdf9a60065b | 67 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | if (deepsleep_mode == false) { |
kadonotakashi | 0:8fdf9a60065b | 70 | sleep_manager_lock_deep_sleep(); |
kadonotakashi | 0:8fdf9a60065b | 71 | } |
kadonotakashi | 0:8fdf9a60065b | 72 | |
kadonotakashi | 0:8fdf9a60065b | 73 | rtc_write(start); |
kadonotakashi | 0:8fdf9a60065b | 74 | |
kadonotakashi | 0:8fdf9a60065b | 75 | timeout.attach(callback, DELAY_4S); |
kadonotakashi | 0:8fdf9a60065b | 76 | |
kadonotakashi | 0:8fdf9a60065b | 77 | TEST_ASSERT(sleep_manager_can_deep_sleep_test_check() == deepsleep_mode); |
kadonotakashi | 0:8fdf9a60065b | 78 | |
kadonotakashi | 0:8fdf9a60065b | 79 | while (!expired) { |
kadonotakashi | 0:8fdf9a60065b | 80 | sleep(); |
kadonotakashi | 0:8fdf9a60065b | 81 | } |
kadonotakashi | 0:8fdf9a60065b | 82 | |
kadonotakashi | 0:8fdf9a60065b | 83 | const uint32_t stop = rtc_read(); |
kadonotakashi | 0:8fdf9a60065b | 84 | |
kadonotakashi | 0:8fdf9a60065b | 85 | TEST_ASSERT_UINT32_WITHIN(RTC_TOLERANCE, DELAY_4S, stop - start); |
kadonotakashi | 0:8fdf9a60065b | 86 | |
kadonotakashi | 0:8fdf9a60065b | 87 | timeout.detach(); |
kadonotakashi | 0:8fdf9a60065b | 88 | |
kadonotakashi | 0:8fdf9a60065b | 89 | if (deepsleep_mode == false) { |
kadonotakashi | 0:8fdf9a60065b | 90 | sleep_manager_unlock_deep_sleep(); |
kadonotakashi | 0:8fdf9a60065b | 91 | } |
kadonotakashi | 0:8fdf9a60065b | 92 | |
kadonotakashi | 0:8fdf9a60065b | 93 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 94 | } |
kadonotakashi | 0:8fdf9a60065b | 95 | #endif |
kadonotakashi | 0:8fdf9a60065b | 96 | |
kadonotakashi | 0:8fdf9a60065b | 97 | /* Test that ::rtc_init can be called multiple times. */ |
kadonotakashi | 0:8fdf9a60065b | 98 | void rtc_init_test() |
kadonotakashi | 0:8fdf9a60065b | 99 | { |
kadonotakashi | 0:8fdf9a60065b | 100 | for (int i = 0; i < 10; i++) { |
kadonotakashi | 0:8fdf9a60065b | 101 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 102 | } |
kadonotakashi | 0:8fdf9a60065b | 103 | |
kadonotakashi | 0:8fdf9a60065b | 104 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 105 | } |
kadonotakashi | 0:8fdf9a60065b | 106 | |
kadonotakashi | 0:8fdf9a60065b | 107 | #if DEVICE_LPTICKER |
kadonotakashi | 0:8fdf9a60065b | 108 | /** Test that the RTC keeps counting in the various sleep modes. */ |
kadonotakashi | 0:8fdf9a60065b | 109 | |
kadonotakashi | 0:8fdf9a60065b | 110 | void rtc_sleep_test() |
kadonotakashi | 0:8fdf9a60065b | 111 | { |
kadonotakashi | 0:8fdf9a60065b | 112 | /* Test sleep mode. */ |
kadonotakashi | 0:8fdf9a60065b | 113 | rtc_sleep_test_support(false); |
kadonotakashi | 0:8fdf9a60065b | 114 | |
kadonotakashi | 0:8fdf9a60065b | 115 | /* Test deep-sleep mode. */ |
kadonotakashi | 0:8fdf9a60065b | 116 | rtc_sleep_test_support(true); |
kadonotakashi | 0:8fdf9a60065b | 117 | } |
kadonotakashi | 0:8fdf9a60065b | 118 | #endif |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | /* Test that the RTC keeps counting even after ::rtc_free has been called. */ |
kadonotakashi | 0:8fdf9a60065b | 121 | void rtc_persist_test() |
kadonotakashi | 0:8fdf9a60065b | 122 | { |
kadonotakashi | 0:8fdf9a60065b | 123 | const uint32_t start = 100; |
kadonotakashi | 0:8fdf9a60065b | 124 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 125 | rtc_write(start); |
kadonotakashi | 0:8fdf9a60065b | 126 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 127 | |
kadonotakashi | 0:8fdf9a60065b | 128 | wait(WAIT_TIME); |
kadonotakashi | 0:8fdf9a60065b | 129 | |
kadonotakashi | 0:8fdf9a60065b | 130 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 131 | const uint32_t stop = rtc_read(); |
kadonotakashi | 0:8fdf9a60065b | 132 | const int enabled = rtc_isenabled(); |
kadonotakashi | 0:8fdf9a60065b | 133 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 134 | |
kadonotakashi | 0:8fdf9a60065b | 135 | TEST_ASSERT_TRUE(enabled); |
kadonotakashi | 0:8fdf9a60065b | 136 | TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start); |
kadonotakashi | 0:8fdf9a60065b | 137 | } |
kadonotakashi | 0:8fdf9a60065b | 138 | |
kadonotakashi | 0:8fdf9a60065b | 139 | /* Test time does not glitch backwards due to an incorrectly implemented ripple counter driver. */ |
kadonotakashi | 0:8fdf9a60065b | 140 | void rtc_glitch_test() |
kadonotakashi | 0:8fdf9a60065b | 141 | { |
kadonotakashi | 0:8fdf9a60065b | 142 | const uint32_t start = 0xffffe; |
kadonotakashi | 0:8fdf9a60065b | 143 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 144 | |
kadonotakashi | 0:8fdf9a60065b | 145 | rtc_write(start); |
kadonotakashi | 0:8fdf9a60065b | 146 | uint32_t last = start; |
kadonotakashi | 0:8fdf9a60065b | 147 | while (last < start + 4) { |
kadonotakashi | 0:8fdf9a60065b | 148 | const uint32_t cur = rtc_read(); |
kadonotakashi | 0:8fdf9a60065b | 149 | TEST_ASSERT(cur >= last); |
kadonotakashi | 0:8fdf9a60065b | 150 | last = cur; |
kadonotakashi | 0:8fdf9a60065b | 151 | } |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 154 | } |
kadonotakashi | 0:8fdf9a60065b | 155 | |
kadonotakashi | 0:8fdf9a60065b | 156 | /* Test that the RTC correctly handles different time values. */ |
kadonotakashi | 0:8fdf9a60065b | 157 | void rtc_range_test() |
kadonotakashi | 0:8fdf9a60065b | 158 | { |
kadonotakashi | 0:8fdf9a60065b | 159 | static const uint32_t starts[] = { |
kadonotakashi | 0:8fdf9a60065b | 160 | 0x00000000, |
kadonotakashi | 0:8fdf9a60065b | 161 | 0xEFFFFFFF, |
kadonotakashi | 0:8fdf9a60065b | 162 | 0x00001000, |
kadonotakashi | 0:8fdf9a60065b | 163 | 0x00010000, |
kadonotakashi | 0:8fdf9a60065b | 164 | }; |
kadonotakashi | 0:8fdf9a60065b | 165 | |
kadonotakashi | 0:8fdf9a60065b | 166 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 167 | for (uint32_t i = 0; i < sizeof(starts) / sizeof(starts[0]); i++) { |
kadonotakashi | 0:8fdf9a60065b | 168 | const uint32_t start = starts[i]; |
kadonotakashi | 0:8fdf9a60065b | 169 | rtc_write(start); |
kadonotakashi | 0:8fdf9a60065b | 170 | wait(WAIT_TIME); |
kadonotakashi | 0:8fdf9a60065b | 171 | const uint32_t stop = rtc_read(); |
kadonotakashi | 0:8fdf9a60065b | 172 | TEST_ASSERT_UINT32_WITHIN(WAIT_TOLERANCE, WAIT_TIME, stop - start); |
kadonotakashi | 0:8fdf9a60065b | 173 | } |
kadonotakashi | 0:8fdf9a60065b | 174 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 175 | } |
kadonotakashi | 0:8fdf9a60065b | 176 | |
kadonotakashi | 0:8fdf9a60065b | 177 | /* Test that the RTC accuracy is at least 10%. */ |
kadonotakashi | 0:8fdf9a60065b | 178 | void rtc_accuracy_test() |
kadonotakashi | 0:8fdf9a60065b | 179 | { |
kadonotakashi | 0:8fdf9a60065b | 180 | Timer timer1; |
kadonotakashi | 0:8fdf9a60065b | 181 | |
kadonotakashi | 0:8fdf9a60065b | 182 | const uint32_t start = 100; |
kadonotakashi | 0:8fdf9a60065b | 183 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 184 | rtc_write(start); |
kadonotakashi | 0:8fdf9a60065b | 185 | |
kadonotakashi | 0:8fdf9a60065b | 186 | timer1.start(); |
kadonotakashi | 0:8fdf9a60065b | 187 | while (rtc_read() < (start + DELAY_10S)) { |
kadonotakashi | 0:8fdf9a60065b | 188 | /* Just wait. */ |
kadonotakashi | 0:8fdf9a60065b | 189 | } |
kadonotakashi | 0:8fdf9a60065b | 190 | timer1.stop(); |
kadonotakashi | 0:8fdf9a60065b | 191 | |
kadonotakashi | 0:8fdf9a60065b | 192 | /* RTC accuracy is at least 10%. */ |
kadonotakashi | 0:8fdf9a60065b | 193 | TEST_ASSERT_INT32_WITHIN(TOLERANCE_ACCURACY_US, DELAY_10S * US_PER_SEC, timer1.read_us()); |
kadonotakashi | 0:8fdf9a60065b | 194 | } |
kadonotakashi | 0:8fdf9a60065b | 195 | |
kadonotakashi | 0:8fdf9a60065b | 196 | /* Test that ::rtc_write/::rtc_read functions provides availability to set/get RTC time. */ |
kadonotakashi | 0:8fdf9a60065b | 197 | void rtc_write_read_test() |
kadonotakashi | 0:8fdf9a60065b | 198 | { |
kadonotakashi | 0:8fdf9a60065b | 199 | static const uint32_t rtc_init_val = 100; |
kadonotakashi | 0:8fdf9a60065b | 200 | |
kadonotakashi | 0:8fdf9a60065b | 201 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 202 | |
kadonotakashi | 0:8fdf9a60065b | 203 | for (int i = 0; i < 3; i++) { |
kadonotakashi | 0:8fdf9a60065b | 204 | const uint32_t init_val = (rtc_init_val + i * rtc_init_val); |
kadonotakashi | 0:8fdf9a60065b | 205 | |
kadonotakashi | 0:8fdf9a60065b | 206 | core_util_critical_section_enter(); |
kadonotakashi | 0:8fdf9a60065b | 207 | |
kadonotakashi | 0:8fdf9a60065b | 208 | rtc_write(init_val); |
kadonotakashi | 0:8fdf9a60065b | 209 | const uint32_t read_val = rtc_read(); |
kadonotakashi | 0:8fdf9a60065b | 210 | |
kadonotakashi | 0:8fdf9a60065b | 211 | core_util_critical_section_exit(); |
kadonotakashi | 0:8fdf9a60065b | 212 | |
kadonotakashi | 0:8fdf9a60065b | 213 | /* No tolerance is provided since we should have 1 second to |
kadonotakashi | 0:8fdf9a60065b | 214 | * execute this case after the RTC time is set. |
kadonotakashi | 0:8fdf9a60065b | 215 | */ |
kadonotakashi | 0:8fdf9a60065b | 216 | TEST_ASSERT_EQUAL_UINT32(init_val, read_val); |
kadonotakashi | 0:8fdf9a60065b | 217 | } |
kadonotakashi | 0:8fdf9a60065b | 218 | |
kadonotakashi | 0:8fdf9a60065b | 219 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 220 | } |
kadonotakashi | 0:8fdf9a60065b | 221 | |
kadonotakashi | 0:8fdf9a60065b | 222 | /* Test that ::is_enabled function returns 1 if the RTC is counting and the time has been set. */ |
kadonotakashi | 0:8fdf9a60065b | 223 | void rtc_enabled_test() |
kadonotakashi | 0:8fdf9a60065b | 224 | { |
kadonotakashi | 0:8fdf9a60065b | 225 | /* Since some platforms use RTC for low power timer RTC may be already enabled. |
kadonotakashi | 0:8fdf9a60065b | 226 | * Because of that we will only verify if rtc_isenabled() returns 1 in case when init is done |
kadonotakashi | 0:8fdf9a60065b | 227 | * and RTC time is set. |
kadonotakashi | 0:8fdf9a60065b | 228 | */ |
kadonotakashi | 0:8fdf9a60065b | 229 | |
kadonotakashi | 0:8fdf9a60065b | 230 | rtc_init(); |
kadonotakashi | 0:8fdf9a60065b | 231 | rtc_write(0); |
kadonotakashi | 0:8fdf9a60065b | 232 | TEST_ASSERT_EQUAL_INT(1, rtc_isenabled()); |
kadonotakashi | 0:8fdf9a60065b | 233 | rtc_free(); |
kadonotakashi | 0:8fdf9a60065b | 234 | } |
kadonotakashi | 0:8fdf9a60065b | 235 | |
kadonotakashi | 0:8fdf9a60065b | 236 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 237 | Case("RTC - init", rtc_init_test), |
kadonotakashi | 0:8fdf9a60065b | 238 | #if DEVICE_LPTICKER |
kadonotakashi | 0:8fdf9a60065b | 239 | Case("RTC - sleep", rtc_sleep_test), |
kadonotakashi | 0:8fdf9a60065b | 240 | #endif |
kadonotakashi | 0:8fdf9a60065b | 241 | Case("RTC - persist", rtc_persist_test), |
kadonotakashi | 0:8fdf9a60065b | 242 | Case("RTC - glitch", rtc_glitch_test), |
kadonotakashi | 0:8fdf9a60065b | 243 | Case("RTC - range", rtc_range_test), |
kadonotakashi | 0:8fdf9a60065b | 244 | Case("RTC - accuracy", rtc_accuracy_test), |
kadonotakashi | 0:8fdf9a60065b | 245 | Case("RTC - write/read", rtc_write_read_test), |
kadonotakashi | 0:8fdf9a60065b | 246 | Case("RTC - enabled", rtc_enabled_test), |
kadonotakashi | 0:8fdf9a60065b | 247 | }; |
kadonotakashi | 0:8fdf9a60065b | 248 | |
kadonotakashi | 0:8fdf9a60065b | 249 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 250 | { |
kadonotakashi | 0:8fdf9a60065b | 251 | GREENTEA_SETUP(60, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 252 | return greentea_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 253 | } |
kadonotakashi | 0:8fdf9a60065b | 254 | |
kadonotakashi | 0:8fdf9a60065b | 255 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
kadonotakashi | 0:8fdf9a60065b | 256 | |
kadonotakashi | 0:8fdf9a60065b | 257 | int main() |
kadonotakashi | 0:8fdf9a60065b | 258 | { |
kadonotakashi | 0:8fdf9a60065b | 259 | Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 260 | } |