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 #include "mbed.h"
kadonotakashi 0:8fdf9a60065b 18 #include "greentea-client/test_env.h"
kadonotakashi 0:8fdf9a60065b 19 #include "unity.h"
kadonotakashi 0:8fdf9a60065b 20 #include "utest.h"
kadonotakashi 0:8fdf9a60065b 21 #include "rtos.h"
kadonotakashi 0:8fdf9a60065b 22 #include "rtc_api.h"
kadonotakashi 0:8fdf9a60065b 23
kadonotakashi 0:8fdf9a60065b 24 #if !DEVICE_RTC || !DEVICE_USTICKER
kadonotakashi 0:8fdf9a60065b 25 #error [NOT_SUPPORTED] test not supported
kadonotakashi 0:8fdf9a60065b 26 #endif
kadonotakashi 0:8fdf9a60065b 27
kadonotakashi 0:8fdf9a60065b 28 using namespace utest::v1;
kadonotakashi 0:8fdf9a60065b 29
kadonotakashi 0:8fdf9a60065b 30 /* On some boards RTC counter can not be
kadonotakashi 0:8fdf9a60065b 31 * Initialised with 0 value in such case
kadonotakashi 0:8fdf9a60065b 32 * drivers sets RTC counter to 1. */
kadonotakashi 0:8fdf9a60065b 33 #define CUSTOM_TIME_0 1
kadonotakashi 0:8fdf9a60065b 34 #define CUSTOM_TIME_1 1256729737
kadonotakashi 0:8fdf9a60065b 35 #define CUSTOM_TIME_2 2147483637
kadonotakashi 0:8fdf9a60065b 36
kadonotakashi 0:8fdf9a60065b 37 #define DELAY_10_SEC 10
kadonotakashi 0:8fdf9a60065b 38 #define MS_PER_SEC 1000
kadonotakashi 0:8fdf9a60065b 39 #define RTC_DELTA 1
kadonotakashi 0:8fdf9a60065b 40
kadonotakashi 0:8fdf9a60065b 41 static volatile int rtc_enabled_ret;
kadonotakashi 0:8fdf9a60065b 42 static volatile time_t rtc_time_val;
kadonotakashi 0:8fdf9a60065b 43 static volatile bool rtc_read_called;
kadonotakashi 0:8fdf9a60065b 44 static volatile bool rtc_write_called;
kadonotakashi 0:8fdf9a60065b 45 static volatile bool rtc_init_called;
kadonotakashi 0:8fdf9a60065b 46 static volatile bool rtc_isenabled_called;
kadonotakashi 0:8fdf9a60065b 47
kadonotakashi 0:8fdf9a60065b 48 /* Stub of RTC read function. */
kadonotakashi 0:8fdf9a60065b 49 static time_t read_rtc_stub(void)
kadonotakashi 0:8fdf9a60065b 50 {
kadonotakashi 0:8fdf9a60065b 51 rtc_read_called = true;
kadonotakashi 0:8fdf9a60065b 52
kadonotakashi 0:8fdf9a60065b 53 return rtc_time_val;
kadonotakashi 0:8fdf9a60065b 54 }
kadonotakashi 0:8fdf9a60065b 55
kadonotakashi 0:8fdf9a60065b 56 /* Stub of RTC write function. */
kadonotakashi 0:8fdf9a60065b 57 static void write_rtc_stub(time_t t)
kadonotakashi 0:8fdf9a60065b 58 {
kadonotakashi 0:8fdf9a60065b 59 rtc_write_called = true;
kadonotakashi 0:8fdf9a60065b 60
kadonotakashi 0:8fdf9a60065b 61 rtc_time_val = t;
kadonotakashi 0:8fdf9a60065b 62 }
kadonotakashi 0:8fdf9a60065b 63
kadonotakashi 0:8fdf9a60065b 64 /* Stub of RTC init function. */
kadonotakashi 0:8fdf9a60065b 65 static void init_rtc_stub(void)
kadonotakashi 0:8fdf9a60065b 66 {
kadonotakashi 0:8fdf9a60065b 67 rtc_init_called = true;
kadonotakashi 0:8fdf9a60065b 68 }
kadonotakashi 0:8fdf9a60065b 69
kadonotakashi 0:8fdf9a60065b 70 /* Stub of RTC isenabled function. */
kadonotakashi 0:8fdf9a60065b 71 static int isenabled_rtc_stub(void)
kadonotakashi 0:8fdf9a60065b 72 {
kadonotakashi 0:8fdf9a60065b 73 rtc_isenabled_called = true;
kadonotakashi 0:8fdf9a60065b 74
kadonotakashi 0:8fdf9a60065b 75 return rtc_enabled_ret;
kadonotakashi 0:8fdf9a60065b 76 }
kadonotakashi 0:8fdf9a60065b 77
kadonotakashi 0:8fdf9a60065b 78 /* This test verifies if attach_rtc provides availability to
kadonotakashi 0:8fdf9a60065b 79 * connect specific RTC driver functions.
kadonotakashi 0:8fdf9a60065b 80 *
kadonotakashi 0:8fdf9a60065b 81 * This is unit test to verify if correct functions are used
kadonotakashi 0:8fdf9a60065b 82 * to support RTC.
kadonotakashi 0:8fdf9a60065b 83 *
kadonotakashi 0:8fdf9a60065b 84 * Given specific RTC driver functions have been attached (stubs).
kadonotakashi 0:8fdf9a60065b 85 * When set_time/time functions are called.
kadonotakashi 0:8fdf9a60065b 86 * Then set_time/time functions use attached RTC functions.
kadonotakashi 0:8fdf9a60065b 87 */
kadonotakashi 0:8fdf9a60065b 88 void test_attach_RTC_stub_funtions()
kadonotakashi 0:8fdf9a60065b 89 {
kadonotakashi 0:8fdf9a60065b 90 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 91
kadonotakashi 0:8fdf9a60065b 92 /* Attache RTC read/write/init/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 93 attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 94
kadonotakashi 0:8fdf9a60065b 95 /* Init stub variables/set to unexpected. */
kadonotakashi 0:8fdf9a60065b 96 rtc_write_called = false;
kadonotakashi 0:8fdf9a60065b 97 rtc_init_called = false;
kadonotakashi 0:8fdf9a60065b 98
kadonotakashi 0:8fdf9a60065b 99 /* Call set_time() function. We expect that init and write RTC stubs
kadonotakashi 0:8fdf9a60065b 100 * will be executed.
kadonotakashi 0:8fdf9a60065b 101 */
kadonotakashi 0:8fdf9a60065b 102 set_time(CUSTOM_TIME_1);
kadonotakashi 0:8fdf9a60065b 103
kadonotakashi 0:8fdf9a60065b 104 /* Verify results. */
kadonotakashi 0:8fdf9a60065b 105 TEST_ASSERT_EQUAL(true, rtc_write_called);
kadonotakashi 0:8fdf9a60065b 106 TEST_ASSERT_EQUAL(true, rtc_init_called);
kadonotakashi 0:8fdf9a60065b 107
kadonotakashi 0:8fdf9a60065b 108 /* Init stub variables/set to unexpected. */
kadonotakashi 0:8fdf9a60065b 109 rtc_time_val = CUSTOM_TIME_1;
kadonotakashi 0:8fdf9a60065b 110 rtc_enabled_ret = true;
kadonotakashi 0:8fdf9a60065b 111 rtc_isenabled_called = false;
kadonotakashi 0:8fdf9a60065b 112 rtc_read_called = false;
kadonotakashi 0:8fdf9a60065b 113
kadonotakashi 0:8fdf9a60065b 114 /* Call time() function. We expect that isenabled and read RTC stubs
kadonotakashi 0:8fdf9a60065b 115 * are be executed.
kadonotakashi 0:8fdf9a60065b 116 */
kadonotakashi 0:8fdf9a60065b 117 time(NULL);
kadonotakashi 0:8fdf9a60065b 118
kadonotakashi 0:8fdf9a60065b 119 /* Verify results. */
kadonotakashi 0:8fdf9a60065b 120 TEST_ASSERT_EQUAL(true, rtc_isenabled_called);
kadonotakashi 0:8fdf9a60065b 121 TEST_ASSERT_EQUAL(true, rtc_read_called);
kadonotakashi 0:8fdf9a60065b 122
kadonotakashi 0:8fdf9a60065b 123 /* This part of the test can be executed only on RTC devices. */
kadonotakashi 0:8fdf9a60065b 124
kadonotakashi 0:8fdf9a60065b 125 /* Restore env. */
kadonotakashi 0:8fdf9a60065b 126 attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
kadonotakashi 0:8fdf9a60065b 127
kadonotakashi 0:8fdf9a60065b 128 /* Set to unexpected. */
kadonotakashi 0:8fdf9a60065b 129 rtc_write_called = false;
kadonotakashi 0:8fdf9a60065b 130 rtc_init_called = false;
kadonotakashi 0:8fdf9a60065b 131 rtc_isenabled_called = false;
kadonotakashi 0:8fdf9a60065b 132 rtc_init_called = false;
kadonotakashi 0:8fdf9a60065b 133
kadonotakashi 0:8fdf9a60065b 134 /* Set time. */
kadonotakashi 0:8fdf9a60065b 135 set_time(CUSTOM_TIME_1);
kadonotakashi 0:8fdf9a60065b 136
kadonotakashi 0:8fdf9a60065b 137 /* Get time. */
kadonotakashi 0:8fdf9a60065b 138 seconds = time(NULL);
kadonotakashi 0:8fdf9a60065b 139
kadonotakashi 0:8fdf9a60065b 140 /* Stub RTC functions should not be called now. */
kadonotakashi 0:8fdf9a60065b 141 TEST_ASSERT_EQUAL(false, rtc_isenabled_called);
kadonotakashi 0:8fdf9a60065b 142 TEST_ASSERT_EQUAL(false, rtc_init_called);
kadonotakashi 0:8fdf9a60065b 143 TEST_ASSERT_EQUAL(false, rtc_write_called);
kadonotakashi 0:8fdf9a60065b 144 TEST_ASSERT_EQUAL(false, rtc_init_called);
kadonotakashi 0:8fdf9a60065b 145
kadonotakashi 0:8fdf9a60065b 146 /* Check if time has been successfully set and retrieved. */
kadonotakashi 0:8fdf9a60065b 147 TEST_ASSERT_UINT32_WITHIN(RTC_DELTA, CUSTOM_TIME_1, seconds);
kadonotakashi 0:8fdf9a60065b 148 }
kadonotakashi 0:8fdf9a60065b 149
kadonotakashi 0:8fdf9a60065b 150 /* This test verifies if attach_rtc provides availability to
kadonotakashi 0:8fdf9a60065b 151 * connect specific RTC driver functions.
kadonotakashi 0:8fdf9a60065b 152 *
kadonotakashi 0:8fdf9a60065b 153 * This is unit test to verify if correct functions are used
kadonotakashi 0:8fdf9a60065b 154 * to support RTC.
kadonotakashi 0:8fdf9a60065b 155 *
kadonotakashi 0:8fdf9a60065b 156 * Given specific RTC driver functions have been attached (original).
kadonotakashi 0:8fdf9a60065b 157 * When set_time/time functions are called.
kadonotakashi 0:8fdf9a60065b 158 * Then set_time/time functions use attached RTC functions.
kadonotakashi 0:8fdf9a60065b 159 */
kadonotakashi 0:8fdf9a60065b 160 void test_attach_RTC_org_funtions()
kadonotakashi 0:8fdf9a60065b 161 {
kadonotakashi 0:8fdf9a60065b 162 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 163
kadonotakashi 0:8fdf9a60065b 164 /* Attache original driver functions. */
kadonotakashi 0:8fdf9a60065b 165 attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
kadonotakashi 0:8fdf9a60065b 166
kadonotakashi 0:8fdf9a60065b 167 /* Set to unexpected. */
kadonotakashi 0:8fdf9a60065b 168 rtc_write_called = false;
kadonotakashi 0:8fdf9a60065b 169 rtc_init_called = false;
kadonotakashi 0:8fdf9a60065b 170 rtc_isenabled_called = false;
kadonotakashi 0:8fdf9a60065b 171 rtc_init_called = false;
kadonotakashi 0:8fdf9a60065b 172
kadonotakashi 0:8fdf9a60065b 173 /* Set time. */
kadonotakashi 0:8fdf9a60065b 174 set_time(CUSTOM_TIME_1);
kadonotakashi 0:8fdf9a60065b 175
kadonotakashi 0:8fdf9a60065b 176 /* Get time. */
kadonotakashi 0:8fdf9a60065b 177 seconds = time(NULL);
kadonotakashi 0:8fdf9a60065b 178
kadonotakashi 0:8fdf9a60065b 179 /* Stub RTC functions should not be called now. */
kadonotakashi 0:8fdf9a60065b 180 TEST_ASSERT_EQUAL(false, rtc_isenabled_called);
kadonotakashi 0:8fdf9a60065b 181 TEST_ASSERT_EQUAL(false, rtc_init_called);
kadonotakashi 0:8fdf9a60065b 182 TEST_ASSERT_EQUAL(false, rtc_write_called);
kadonotakashi 0:8fdf9a60065b 183 TEST_ASSERT_EQUAL(false, rtc_init_called);
kadonotakashi 0:8fdf9a60065b 184
kadonotakashi 0:8fdf9a60065b 185 /* Check if time has been successfully set and retrieved. */
kadonotakashi 0:8fdf9a60065b 186 TEST_ASSERT_UINT32_WITHIN(RTC_DELTA, CUSTOM_TIME_1, seconds);
kadonotakashi 0:8fdf9a60065b 187 }
kadonotakashi 0:8fdf9a60065b 188
kadonotakashi 0:8fdf9a60065b 189 /* This test verifies if time() function returns
kadonotakashi 0:8fdf9a60065b 190 * current time when all RTC functions are
kadonotakashi 0:8fdf9a60065b 191 * defined and RTC is enabled.
kadonotakashi 0:8fdf9a60065b 192 *
kadonotakashi 0:8fdf9a60065b 193 * Note: Stubs are used instead of original RTC functions.
kadonotakashi 0:8fdf9a60065b 194 *
kadonotakashi 0:8fdf9a60065b 195 * Given environment has RTC functions defined and RTC is enabled.
kadonotakashi 0:8fdf9a60065b 196 * When time() functions is called.
kadonotakashi 0:8fdf9a60065b 197 * Then current time is returned.
kadonotakashi 0:8fdf9a60065b 198 */
kadonotakashi 0:8fdf9a60065b 199 void test_time_RTC_func_defined_RTC_is_enabled()
kadonotakashi 0:8fdf9a60065b 200 {
kadonotakashi 0:8fdf9a60065b 201 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 202
kadonotakashi 0:8fdf9a60065b 203 /* Attache RTC read/write/init/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 204 attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 205
kadonotakashi 0:8fdf9a60065b 206 /* Simulate that RTC is enabled. */
kadonotakashi 0:8fdf9a60065b 207 rtc_enabled_ret = true;
kadonotakashi 0:8fdf9a60065b 208
kadonotakashi 0:8fdf9a60065b 209 /* Simulate current time. */
kadonotakashi 0:8fdf9a60065b 210 rtc_time_val = CUSTOM_TIME_1;
kadonotakashi 0:8fdf9a60065b 211
kadonotakashi 0:8fdf9a60065b 212 /* Try to get current time. */
kadonotakashi 0:8fdf9a60065b 213 seconds = time(NULL);
kadonotakashi 0:8fdf9a60065b 214
kadonotakashi 0:8fdf9a60065b 215 /* Check if expected value has been returned. */
kadonotakashi 0:8fdf9a60065b 216 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
kadonotakashi 0:8fdf9a60065b 217 }
kadonotakashi 0:8fdf9a60065b 218
kadonotakashi 0:8fdf9a60065b 219 /* This test verifies if time() function resets time
kadonotakashi 0:8fdf9a60065b 220 * when RTC functions are defined and RTC is disabled.
kadonotakashi 0:8fdf9a60065b 221 *
kadonotakashi 0:8fdf9a60065b 222 * Note: Stubs are used instead of original RTC functions.
kadonotakashi 0:8fdf9a60065b 223 *
kadonotakashi 0:8fdf9a60065b 224 * Given environment has RTC functions defined and RTC is disabled.
kadonotakashi 0:8fdf9a60065b 225 * When time() functions is called.
kadonotakashi 0:8fdf9a60065b 226 * Then function result is 0.
kadonotakashi 0:8fdf9a60065b 227 */
kadonotakashi 0:8fdf9a60065b 228 void test_time_RTC_func_defined_RTC_is_disabled()
kadonotakashi 0:8fdf9a60065b 229 {
kadonotakashi 0:8fdf9a60065b 230 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 231
kadonotakashi 0:8fdf9a60065b 232 /* Attache RTC read/write/init/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 233 attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 234
kadonotakashi 0:8fdf9a60065b 235 /* Simulate that RTC is disabled. */
kadonotakashi 0:8fdf9a60065b 236 rtc_enabled_ret = false;
kadonotakashi 0:8fdf9a60065b 237
kadonotakashi 0:8fdf9a60065b 238 /* Simulate current time. */
kadonotakashi 0:8fdf9a60065b 239 rtc_time_val = CUSTOM_TIME_1;
kadonotakashi 0:8fdf9a60065b 240
kadonotakashi 0:8fdf9a60065b 241 /* Try to get current time. */
kadonotakashi 0:8fdf9a60065b 242 seconds = time(NULL);
kadonotakashi 0:8fdf9a60065b 243
kadonotakashi 0:8fdf9a60065b 244 /* Check if expected value has been returned. */
kadonotakashi 0:8fdf9a60065b 245 TEST_ASSERT_EQUAL(0, seconds);
kadonotakashi 0:8fdf9a60065b 246 }
kadonotakashi 0:8fdf9a60065b 247
kadonotakashi 0:8fdf9a60065b 248 /* This test verifies if time() function can be successfully
kadonotakashi 0:8fdf9a60065b 249 * executed when isenabled RTC function is undefined.
kadonotakashi 0:8fdf9a60065b 250 *
kadonotakashi 0:8fdf9a60065b 251 * Note: Stubs are used instead of original RTC functions.
kadonotakashi 0:8fdf9a60065b 252 *
kadonotakashi 0:8fdf9a60065b 253 * Given environment has isenabled RTC function undefined.
kadonotakashi 0:8fdf9a60065b 254 * When time() functions is called.
kadonotakashi 0:8fdf9a60065b 255 * Then current time is returned.
kadonotakashi 0:8fdf9a60065b 256 */
kadonotakashi 0:8fdf9a60065b 257 void test_time_isenabled_RTC_func_undefined()
kadonotakashi 0:8fdf9a60065b 258 {
kadonotakashi 0:8fdf9a60065b 259 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 260
kadonotakashi 0:8fdf9a60065b 261 /* Attache RTC read/write/init stubs. */
kadonotakashi 0:8fdf9a60065b 262 attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, NULL);
kadonotakashi 0:8fdf9a60065b 263
kadonotakashi 0:8fdf9a60065b 264 /* Simulate current time. */
kadonotakashi 0:8fdf9a60065b 265 rtc_time_val = CUSTOM_TIME_1;
kadonotakashi 0:8fdf9a60065b 266
kadonotakashi 0:8fdf9a60065b 267 /* Try to get current time. */
kadonotakashi 0:8fdf9a60065b 268 seconds = time(NULL);
kadonotakashi 0:8fdf9a60065b 269
kadonotakashi 0:8fdf9a60065b 270 /* Check if expected value has been returned. */
kadonotakashi 0:8fdf9a60065b 271 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
kadonotakashi 0:8fdf9a60065b 272 }
kadonotakashi 0:8fdf9a60065b 273
kadonotakashi 0:8fdf9a60065b 274 /* This test verifies if time() function returns -1 if
kadonotakashi 0:8fdf9a60065b 275 * read RTC function is undefined.
kadonotakashi 0:8fdf9a60065b 276 *
kadonotakashi 0:8fdf9a60065b 277 * Note: Stubs are used instead of original RTC functions.
kadonotakashi 0:8fdf9a60065b 278 *
kadonotakashi 0:8fdf9a60065b 279 * Given environment has read RTC function undefined.
kadonotakashi 0:8fdf9a60065b 280 * When time() functions is called.
kadonotakashi 0:8fdf9a60065b 281 * Then -1 is returned.
kadonotakashi 0:8fdf9a60065b 282 */
kadonotakashi 0:8fdf9a60065b 283 void test_time_read_RTC_func_undefined()
kadonotakashi 0:8fdf9a60065b 284 {
kadonotakashi 0:8fdf9a60065b 285 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 286
kadonotakashi 0:8fdf9a60065b 287 /* Attache RTC write/init/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 288 attach_rtc(NULL, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 289
kadonotakashi 0:8fdf9a60065b 290 /* Simulate current time. */
kadonotakashi 0:8fdf9a60065b 291 rtc_time_val = CUSTOM_TIME_1;
kadonotakashi 0:8fdf9a60065b 292
kadonotakashi 0:8fdf9a60065b 293 /* Try to get current time. */
kadonotakashi 0:8fdf9a60065b 294 seconds = time(NULL);
kadonotakashi 0:8fdf9a60065b 295
kadonotakashi 0:8fdf9a60065b 296 /* Check if expected value has been returned. */
kadonotakashi 0:8fdf9a60065b 297 TEST_ASSERT_EQUAL((time_t) -1, seconds);
kadonotakashi 0:8fdf9a60065b 298 }
kadonotakashi 0:8fdf9a60065b 299
kadonotakashi 0:8fdf9a60065b 300 /* This test verifies if time() function stores
kadonotakashi 0:8fdf9a60065b 301 * the result in given time buffer (if specified).
kadonotakashi 0:8fdf9a60065b 302 *
kadonotakashi 0:8fdf9a60065b 303 * Note: Stubs are used instead original RTC functions.
kadonotakashi 0:8fdf9a60065b 304 * Other test cases calls time() routine with
kadonotakashi 0:8fdf9a60065b 305 * undefined time buffer.
kadonotakashi 0:8fdf9a60065b 306 *
kadonotakashi 0:8fdf9a60065b 307 * Given environment has all RTC function defined, RTC is enabled and time buffer is passed to time() function.
kadonotakashi 0:8fdf9a60065b 308 * When time() functions is called.
kadonotakashi 0:8fdf9a60065b 309 * Then current time is stored in the specified buffer.
kadonotakashi 0:8fdf9a60065b 310 */
kadonotakashi 0:8fdf9a60065b 311 void test_time_called_with_param()
kadonotakashi 0:8fdf9a60065b 312 {
kadonotakashi 0:8fdf9a60065b 313 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 314 time_t buffer = 0;
kadonotakashi 0:8fdf9a60065b 315
kadonotakashi 0:8fdf9a60065b 316 /* Attache RTC read/write/init/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 317 attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 318
kadonotakashi 0:8fdf9a60065b 319 /* Simulate that RTC is enabled. */
kadonotakashi 0:8fdf9a60065b 320 rtc_enabled_ret = true;
kadonotakashi 0:8fdf9a60065b 321
kadonotakashi 0:8fdf9a60065b 322 /* Simulate current time. */
kadonotakashi 0:8fdf9a60065b 323 rtc_time_val = CUSTOM_TIME_1;
kadonotakashi 0:8fdf9a60065b 324
kadonotakashi 0:8fdf9a60065b 325 /* Try to get current time. */
kadonotakashi 0:8fdf9a60065b 326 seconds = time(&buffer);
kadonotakashi 0:8fdf9a60065b 327
kadonotakashi 0:8fdf9a60065b 328 /* Check if expected value has been returned. */
kadonotakashi 0:8fdf9a60065b 329 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
kadonotakashi 0:8fdf9a60065b 330 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, buffer);
kadonotakashi 0:8fdf9a60065b 331 }
kadonotakashi 0:8fdf9a60065b 332
kadonotakashi 0:8fdf9a60065b 333 /* This test verifies if set_time() function inits the RTC
kadonotakashi 0:8fdf9a60065b 334 * and writes current time if RTC functions are defined.
kadonotakashi 0:8fdf9a60065b 335 *
kadonotakashi 0:8fdf9a60065b 336 * Note: Stubs are used instead of original RTC functions.
kadonotakashi 0:8fdf9a60065b 337 *
kadonotakashi 0:8fdf9a60065b 338 * Given environment has RTC functions defined.
kadonotakashi 0:8fdf9a60065b 339 * When set_time() functions is called.
kadonotakashi 0:8fdf9a60065b 340 * Then function initialises RTC and sets RTC time.
kadonotakashi 0:8fdf9a60065b 341 */
kadonotakashi 0:8fdf9a60065b 342 void test_set_time_RTC_func_defined()
kadonotakashi 0:8fdf9a60065b 343 {
kadonotakashi 0:8fdf9a60065b 344 /* Attache RTC read/write/init/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 345 attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 346
kadonotakashi 0:8fdf9a60065b 347 /* Set to unexpected. */
kadonotakashi 0:8fdf9a60065b 348 rtc_time_val = 123;
kadonotakashi 0:8fdf9a60065b 349 rtc_init_called = false;
kadonotakashi 0:8fdf9a60065b 350
kadonotakashi 0:8fdf9a60065b 351 /* Set current time. */
kadonotakashi 0:8fdf9a60065b 352 rtc_time_val = 123;
kadonotakashi 0:8fdf9a60065b 353
kadonotakashi 0:8fdf9a60065b 354 /* Set new RTC time. */
kadonotakashi 0:8fdf9a60065b 355 set_time(CUSTOM_TIME_1);
kadonotakashi 0:8fdf9a60065b 356
kadonotakashi 0:8fdf9a60065b 357 /* Check if RTC init has been performed and RTC time has been updated. */
kadonotakashi 0:8fdf9a60065b 358 TEST_ASSERT_EQUAL(true, rtc_init_called);
kadonotakashi 0:8fdf9a60065b 359 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, time(NULL));
kadonotakashi 0:8fdf9a60065b 360 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, rtc_time_val);
kadonotakashi 0:8fdf9a60065b 361 }
kadonotakashi 0:8fdf9a60065b 362
kadonotakashi 0:8fdf9a60065b 363 /* This test verifies if set_time() function can be
kadonotakashi 0:8fdf9a60065b 364 * successfully executed when init RTC function is undefined.
kadonotakashi 0:8fdf9a60065b 365 *
kadonotakashi 0:8fdf9a60065b 366 * Note: Stubs are used instead of original RTC functions.
kadonotakashi 0:8fdf9a60065b 367 *
kadonotakashi 0:8fdf9a60065b 368 * Given environment has init RTC function undefined.
kadonotakashi 0:8fdf9a60065b 369 * When set_time() functions is called.
kadonotakashi 0:8fdf9a60065b 370 * Then function sets RTC time.
kadonotakashi 0:8fdf9a60065b 371 */
kadonotakashi 0:8fdf9a60065b 372 void test_set_time_init_RTC_func_undefined()
kadonotakashi 0:8fdf9a60065b 373 {
kadonotakashi 0:8fdf9a60065b 374 /* Attache RTC read/write/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 375 attach_rtc(read_rtc_stub, write_rtc_stub, NULL, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 376
kadonotakashi 0:8fdf9a60065b 377 /* Set to unexpected. */
kadonotakashi 0:8fdf9a60065b 378 rtc_time_val = 123;
kadonotakashi 0:8fdf9a60065b 379
kadonotakashi 0:8fdf9a60065b 380 /* Set new RTC time. */
kadonotakashi 0:8fdf9a60065b 381 set_time(CUSTOM_TIME_1);
kadonotakashi 0:8fdf9a60065b 382
kadonotakashi 0:8fdf9a60065b 383 /* Check if RTC time has been updated. */
kadonotakashi 0:8fdf9a60065b 384 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, time(NULL));
kadonotakashi 0:8fdf9a60065b 385 TEST_ASSERT_EQUAL(CUSTOM_TIME_1, rtc_time_val);
kadonotakashi 0:8fdf9a60065b 386 }
kadonotakashi 0:8fdf9a60065b 387
kadonotakashi 0:8fdf9a60065b 388 /* This test verifies if set_time() function can be
kadonotakashi 0:8fdf9a60065b 389 * successfully executed when write RTC function is undefined.
kadonotakashi 0:8fdf9a60065b 390 *
kadonotakashi 0:8fdf9a60065b 391 * Note: Stubs are used instead original RTC functions.
kadonotakashi 0:8fdf9a60065b 392 *
kadonotakashi 0:8fdf9a60065b 393 * Given environemt has write RTC function undefined.
kadonotakashi 0:8fdf9a60065b 394 * When set_time() function is called.
kadonotakashi 0:8fdf9a60065b 395 * Then function inits RTC and does not modify RTC time.
kadonotakashi 0:8fdf9a60065b 396 */
kadonotakashi 0:8fdf9a60065b 397 void test_set_time_write_RTC_func_undefined()
kadonotakashi 0:8fdf9a60065b 398 {
kadonotakashi 0:8fdf9a60065b 399 /* Attache RTC read/write/init/isenabled stubs. */
kadonotakashi 0:8fdf9a60065b 400 attach_rtc(read_rtc_stub, NULL, init_rtc_stub, isenabled_rtc_stub);
kadonotakashi 0:8fdf9a60065b 401
kadonotakashi 0:8fdf9a60065b 402 /* Set to unexpected. */
kadonotakashi 0:8fdf9a60065b 403 rtc_time_val = 123;
kadonotakashi 0:8fdf9a60065b 404 rtc_init_called = false;
kadonotakashi 0:8fdf9a60065b 405
kadonotakashi 0:8fdf9a60065b 406 /* Set new RTC time. */
kadonotakashi 0:8fdf9a60065b 407 set_time(CUSTOM_TIME_1);
kadonotakashi 0:8fdf9a60065b 408
kadonotakashi 0:8fdf9a60065b 409 /* Check if RTC has been initialized and RTC time has not been updated. */
kadonotakashi 0:8fdf9a60065b 410 TEST_ASSERT_EQUAL(true, rtc_init_called);
kadonotakashi 0:8fdf9a60065b 411 TEST_ASSERT_EQUAL(123, time(NULL));
kadonotakashi 0:8fdf9a60065b 412 TEST_ASSERT_EQUAL(123, rtc_time_val);
kadonotakashi 0:8fdf9a60065b 413 }
kadonotakashi 0:8fdf9a60065b 414
kadonotakashi 0:8fdf9a60065b 415 /* This test verifies if RTC time can be successfully set.
kadonotakashi 0:8fdf9a60065b 416 *
kadonotakashi 0:8fdf9a60065b 417 * Note: Original RTC functions are used in this test.
kadonotakashi 0:8fdf9a60065b 418 *
kadonotakashi 0:8fdf9a60065b 419 * Given environment has RTC available.
kadonotakashi 0:8fdf9a60065b 420 * When set_time() functions is called.
kadonotakashi 0:8fdf9a60065b 421 * Then RTC time is retrieved.
kadonotakashi 0:8fdf9a60065b 422 */
kadonotakashi 0:8fdf9a60065b 423 template<uint32_t timeValue>
kadonotakashi 0:8fdf9a60065b 424 void test_functional_set()
kadonotakashi 0:8fdf9a60065b 425 {
kadonotakashi 0:8fdf9a60065b 426 /* Set original RTC functions. */
kadonotakashi 0:8fdf9a60065b 427 attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
kadonotakashi 0:8fdf9a60065b 428
kadonotakashi 0:8fdf9a60065b 429 /* Set new RTC time. */
kadonotakashi 0:8fdf9a60065b 430 set_time(timeValue);
kadonotakashi 0:8fdf9a60065b 431
kadonotakashi 0:8fdf9a60065b 432 /* Get current time and verify that new value has been set. */
kadonotakashi 0:8fdf9a60065b 433 TEST_ASSERT_UINT32_WITHIN(1, timeValue, time(NULL));
kadonotakashi 0:8fdf9a60065b 434 }
kadonotakashi 0:8fdf9a60065b 435
kadonotakashi 0:8fdf9a60065b 436 /* This test verifies if RTC counts seconds.
kadonotakashi 0:8fdf9a60065b 437 *
kadonotakashi 0:8fdf9a60065b 438 * Note: Original RTC functions are used in this test.
kadonotakashi 0:8fdf9a60065b 439 *
kadonotakashi 0:8fdf9a60065b 440 * Given RTC has time set.
kadonotakashi 0:8fdf9a60065b 441 * When some time has passed (seconds).
kadonotakashi 0:8fdf9a60065b 442 * Then RTC time is updated.
kadonotakashi 0:8fdf9a60065b 443 */
kadonotakashi 0:8fdf9a60065b 444 void test_functional_count()
kadonotakashi 0:8fdf9a60065b 445 {
kadonotakashi 0:8fdf9a60065b 446 time_t seconds = 0;
kadonotakashi 0:8fdf9a60065b 447
kadonotakashi 0:8fdf9a60065b 448 /* Set original RTC functions. */
kadonotakashi 0:8fdf9a60065b 449 attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
kadonotakashi 0:8fdf9a60065b 450
kadonotakashi 0:8fdf9a60065b 451 /* Set new RTC time. */
kadonotakashi 0:8fdf9a60065b 452 set_time(CUSTOM_TIME_2);
kadonotakashi 0:8fdf9a60065b 453
kadonotakashi 0:8fdf9a60065b 454 /* Wait 10 sec. */
kadonotakashi 0:8fdf9a60065b 455 wait_ms(DELAY_10_SEC * MS_PER_SEC);
kadonotakashi 0:8fdf9a60065b 456
kadonotakashi 0:8fdf9a60065b 457 /* Get time. */
kadonotakashi 0:8fdf9a60065b 458 seconds = time(NULL);
kadonotakashi 0:8fdf9a60065b 459
kadonotakashi 0:8fdf9a60065b 460 /* Verify that RTC counts seconds. */
kadonotakashi 0:8fdf9a60065b 461 TEST_ASSERT_UINT_WITHIN(RTC_DELTA, (unsigned int)seconds, CUSTOM_TIME_2 + DELAY_10_SEC);
kadonotakashi 0:8fdf9a60065b 462 }
kadonotakashi 0:8fdf9a60065b 463
kadonotakashi 0:8fdf9a60065b 464 utest::v1::status_t test_setup(const size_t number_of_cases)
kadonotakashi 0:8fdf9a60065b 465 {
kadonotakashi 0:8fdf9a60065b 466 GREENTEA_SETUP(20, "default_auto");
kadonotakashi 0:8fdf9a60065b 467 return verbose_test_setup_handler(number_of_cases);
kadonotakashi 0:8fdf9a60065b 468 }
kadonotakashi 0:8fdf9a60065b 469
kadonotakashi 0:8fdf9a60065b 470 Case cases[] = {
kadonotakashi 0:8fdf9a60065b 471 Case("Unit Test: attach stub RTC functions.", test_attach_RTC_stub_funtions),
kadonotakashi 0:8fdf9a60065b 472 Case("Unit Test: attach original RTC functions.", test_attach_RTC_org_funtions),
kadonotakashi 0:8fdf9a60065b 473
kadonotakashi 0:8fdf9a60065b 474 Case("Unit Test: time() - RTC functions are defined, RTC is enabled.", test_time_RTC_func_defined_RTC_is_enabled),
kadonotakashi 0:8fdf9a60065b 475 Case("Unit Test: time() - RTC functions are defined, RTC is disabled.", test_time_RTC_func_defined_RTC_is_disabled),
kadonotakashi 0:8fdf9a60065b 476 Case("Unit Test: time() - isenabled RTC function is undefined.", test_time_isenabled_RTC_func_undefined),
kadonotakashi 0:8fdf9a60065b 477 Case("Unit Test: time() - read RTC function is undefined.", test_time_read_RTC_func_undefined),
kadonotakashi 0:8fdf9a60065b 478 Case("Unit Test: time() - result is stored in given buffer.", test_time_called_with_param),
kadonotakashi 0:8fdf9a60065b 479
kadonotakashi 0:8fdf9a60065b 480 Case("Unit Test: set_time() - RTC functions are defined.", test_set_time_RTC_func_defined),
kadonotakashi 0:8fdf9a60065b 481 Case("Unit Test: set_time() - init RTC function is undefined.", test_set_time_init_RTC_func_undefined),
kadonotakashi 0:8fdf9a60065b 482 Case("Unit Test: set_time() - write RTC function is undefined.", test_set_time_write_RTC_func_undefined),
kadonotakashi 0:8fdf9a60065b 483
kadonotakashi 0:8fdf9a60065b 484 Case("Functional Test: set time - CUSTOM_TIME_0.", test_functional_set<CUSTOM_TIME_0>),
kadonotakashi 0:8fdf9a60065b 485 Case("Functional Test: set time - CUSTOM_TIME_1.", test_functional_set<CUSTOM_TIME_1>),
kadonotakashi 0:8fdf9a60065b 486 Case("Functional Test: set time - CUSTOM_TIME_2.", test_functional_set<CUSTOM_TIME_2>),
kadonotakashi 0:8fdf9a60065b 487
kadonotakashi 0:8fdf9a60065b 488 Case("Functional Test: RTC counts seconds.", test_functional_count),
kadonotakashi 0:8fdf9a60065b 489 };
kadonotakashi 0:8fdf9a60065b 490
kadonotakashi 0:8fdf9a60065b 491 Specification specification(test_setup, cases);
kadonotakashi 0:8fdf9a60065b 492
kadonotakashi 0:8fdf9a60065b 493 int main()
kadonotakashi 0:8fdf9a60065b 494 {
kadonotakashi 0:8fdf9a60065b 495 return !Harness::run(specification);
kadonotakashi 0:8fdf9a60065b 496 }