From ed754ecf6dbd74aecabf2d03086bb847d9e061fc Mon Sep 17 00:00:00 2001 From: jing wang Date: Thu, 8 Dec 2016 16:42:36 +0800 Subject: [PATCH] tests: add rtc driver test case the test include 2 ztest cases, verfy below apis: rtc_enable() rtc_disable() rtc_read() rtc_set_config() rtc_set_alarm() rtc_get_pending_int() Change-Id: Ifca2541312c5ec8d450803f1b6e4f5064098e7fe Signed-off-by: jing wang Signed-off-by: Anas Nashif --- tests/drivers/rtc/rtc_basic_api/Makefile | 4 + tests/drivers/rtc/rtc_basic_api/prj.conf | 3 + tests/drivers/rtc/rtc_basic_api/src/Makefile | 3 + tests/drivers/rtc/rtc_basic_api/src/main.c | 32 +++++ .../drivers/rtc/rtc_basic_api/src/test_rtc.h | 36 +++++ .../rtc/rtc_basic_api/src/test_rtc_alarm.c | 126 ++++++++++++++++++ .../rtc/rtc_basic_api/src/test_rtc_calendar.c | 83 ++++++++++++ tests/drivers/rtc/rtc_basic_api/testcase.ini | 4 + 8 files changed, 291 insertions(+) create mode 100644 tests/drivers/rtc/rtc_basic_api/Makefile create mode 100644 tests/drivers/rtc/rtc_basic_api/prj.conf create mode 100644 tests/drivers/rtc/rtc_basic_api/src/Makefile create mode 100644 tests/drivers/rtc/rtc_basic_api/src/main.c create mode 100644 tests/drivers/rtc/rtc_basic_api/src/test_rtc.h create mode 100644 tests/drivers/rtc/rtc_basic_api/src/test_rtc_alarm.c create mode 100644 tests/drivers/rtc/rtc_basic_api/src/test_rtc_calendar.c create mode 100644 tests/drivers/rtc/rtc_basic_api/testcase.ini diff --git a/tests/drivers/rtc/rtc_basic_api/Makefile b/tests/drivers/rtc/rtc_basic_api/Makefile new file mode 100644 index 00000000000..b0b666b6980 --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/Makefile @@ -0,0 +1,4 @@ +BOARD ?= quark_se_c1000_devboard +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/tests/drivers/rtc/rtc_basic_api/prj.conf b/tests/drivers/rtc/rtc_basic_api/prj.conf new file mode 100644 index 00000000000..db4b347f71c --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/prj.conf @@ -0,0 +1,3 @@ +CONFIG_RTC=y +CONFIG_RTC_QMSI=y +CONFIG_ZTEST=y diff --git a/tests/drivers/rtc/rtc_basic_api/src/Makefile b/tests/drivers/rtc/rtc_basic_api/src/Makefile new file mode 100644 index 00000000000..568a4e3253f --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/src/Makefile @@ -0,0 +1,3 @@ +include $(ZEPHYR_BASE)/tests/Makefile.test + +obj-y += main.o test_rtc_calendar.o test_rtc_alarm.o diff --git a/tests/drivers/rtc/rtc_basic_api/src/main.c b/tests/drivers/rtc/rtc_basic_api/src/main.c new file mode 100644 index 00000000000..1c6e5f5f52b --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/src/main.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup t_driver_rtc + * @{ + * @defgroup t_rtc_basic_api test_rtc_basic_api + * @} + */ + +#include + +void test_main(void) +{ + ztest_test_suite(rtc_basic_test, + ztest_unit_test(test_rtc_alarm), + ztest_unit_test(test_rtc_calendar)); + ztest_run_test_suite(rtc_basic_test); +} diff --git a/tests/drivers/rtc/rtc_basic_api/src/test_rtc.h b/tests/drivers/rtc/rtc_basic_api/src/test_rtc.h new file mode 100644 index 00000000000..845dd3f810f --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/src/test_rtc.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * @brief RTC cases header file + * + * Header file for RTC cases + */ + +#ifndef __TEST_RTC_H__ +#define __TEST_RTC_H__ + +#include +#include +#include + +#define RTC_DEVICE_NAME CONFIG_RTC_0_NAME + +void test_rtc_calendar(void); +void test_rtc_alarm(void); + +#endif /* __TEST_RTC_H__ */ diff --git a/tests/drivers/rtc/rtc_basic_api/src/test_rtc_alarm.c b/tests/drivers/rtc/rtc_basic_api/src/test_rtc_alarm.c new file mode 100644 index 00000000000..005def8a5f3 --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/src/test_rtc_alarm.c @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2016 Intel Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @addtogroup t_rtc_basic_api + * @{ + * @defgroup t_rtc_alarm test_rtc_alarm + * @brief TestPurpose: verify RTC alarm work and pending interrupt detected + * @details + * - Test Steps + * -# Enable RTC internal counter + * -# Configure RTC with init_val, alarm_val, cb_fn and enable RTC alarm + * -# Sleep for while waiting for RTC alarm + * -# Reconfigure RTC to alarm 1 second later using rtc_set_alarm() + * -# Sleep for while waiting for RTC alarm + * -# Disable RTC internal counter and repeat previous operations + * - Expected Results + * -# When RTC internal counter is enabled, RTC alarm can be invoked using + * both rtc_set_config() and rtc_set_alarm() + * -# When RTC internal counter is disabled, RTC alarm won't be invoked. + * @} + */ + +#include + +static bool rtc_alarm_up; + +static void rtc_alarm_callback(struct device *rtc_dev) +{ + + TC_PRINT("%s: Invoked\n", __func__); + TC_PRINT("RTC counter: %u\n", rtc_read(rtc_dev)); + + /* Verify rtc_get_pending_int() */ + if (rtc_get_pending_int(rtc_dev)) { + TC_PRINT("Catch pending RTC interrupt\n"); + } else { + TC_PRINT("Fail to catch pending RTC interrupt\n"); + } + + rtc_alarm_up = true; +} + +static int test_alarm(void) +{ + struct rtc_config config; + struct device *rtc = device_get_binding(RTC_DEVICE_NAME); + + if (!rtc) { + TC_PRINT("Cannot get RTC device\n"); + return TC_FAIL; + } + + config.init_val = 0; + config.alarm_enable = 1; + config.alarm_val = RTC_ALARM_SECOND; + config.cb_fn = rtc_alarm_callback; + + rtc_enable(rtc); + + /* 1. Verify rtc_set_config() */ + rtc_alarm_up = false; + if (rtc_set_config(rtc, &config)) { + TC_ERROR("Failed to config RTC alarm\n"); + return TC_FAIL; + } + + k_sleep(1500); + + if (!rtc_alarm_up) { + TC_PRINT("RTC alarm doesn't work well\n"); + return TC_FAIL; + } + + /* 2. Verify rtc_set_alarm() */ + rtc_alarm_up = false; + if (rtc_set_alarm(rtc, rtc_read(rtc) + RTC_ALARM_SECOND)) { + TC_PRINT("Failed to set RTC Alarm\n"); + return TC_FAIL; + } + + k_sleep(1500); + + if (!rtc_alarm_up) { + TC_PRINT("RTC alarm doesn't work well\n"); + return TC_FAIL; + } + + /* 3. Verify RTC Alarm disabled after disable internal counter */ + rtc_disable(rtc); + rtc_alarm_up = false; + + if (rtc_set_alarm(rtc, rtc_read(rtc) + RTC_ALARM_SECOND)) { + TC_PRINT("Failed to set RTC Alarm\n"); + return TC_FAIL; + } + + k_sleep(1500); + + if (rtc_alarm_up) { + TC_PRINT("Failed to disable RTC Alarm\n"); + return TC_FAIL; + } + + TC_PRINT("RTC alarm works well\n"); + + return TC_PASS; +} + +void test_rtc_alarm(void) +{ + assert_true(test_alarm() == TC_PASS, NULL); +} diff --git a/tests/drivers/rtc/rtc_basic_api/src/test_rtc_calendar.c b/tests/drivers/rtc/rtc_basic_api/src/test_rtc_calendar.c new file mode 100644 index 00000000000..ae731bdbaea --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/src/test_rtc_calendar.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016 Intel Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * @addtogroup t_rtc_basic_api + * @{ + * @defgroup t_rtc_calendar test_rtc_calendar + * @brief TestPurpose: verify RTC internal counter enable/disable/read work + * @details + * - Test Steps + * -# Enable RTC internal counter + * -# Read RTC internal counter and sleep for a while + * -# Read RTC internal counter again and compare it with the previous value + * -# Disable RTC internal counter and repeat previous operations + * - Expected Results + * -# When enabled, the values read from RTC internal counter increase. + * -# When disabled, the values read from RTC internal counter don't change. + * @} + */ + +#include + +static int test_task(void) +{ + uint32_t val_1 = 0; + uint32_t val_2 = 0; + struct device *rtc = device_get_binding(RTC_DEVICE_NAME); + + if (!rtc) { + TC_PRINT("Cannot get RTC device\n"); + return TC_FAIL; + } + + /* 1. Verify rtc_enable() */ + rtc_enable(rtc); + + /* 2. Verify rtc_enable() */ + val_1 = rtc_read(rtc); + k_sleep(2000); + val_2 = rtc_read(rtc); + + TC_PRINT("val_1: %u, val_2: %u, delta: %lu:%lu\n", + val_1, val_2, + (val_2 - val_1) / RTC_ALARM_SECOND, + (val_2 - val_1) % RTC_ALARM_SECOND); + + if (val_2 <= val_1) { + TC_PRINT("RTC doesn't work well\n"); + return TC_FAIL; + } + + /* 3. Verify rtc_disable() */ + rtc_disable(rtc); + + val_1 = rtc_read(rtc); + k_sleep(1000); + val_2 = rtc_read(rtc); + + if (val_2 != val_1) { + TC_PRINT("Fail to disable RTC\n"); + return TC_FAIL; + } + + return TC_PASS; +} + +void test_rtc_calendar(void) +{ + assert_true((test_task() == TC_PASS), NULL); +} diff --git a/tests/drivers/rtc/rtc_basic_api/testcase.ini b/tests/drivers/rtc/rtc_basic_api/testcase.ini new file mode 100644 index 00000000000..e0e450fd264 --- /dev/null +++ b/tests/drivers/rtc/rtc_basic_api/testcase.ini @@ -0,0 +1,4 @@ +[test_rtc] +tags = drivers +arch_whitelist = x86 arc +platform_whitelist = quark_se_c1000_devboard arduino_101 quark_d2000_crb