tests: add zephyr counter and timer api test

The commit verifies below aon counter and timer apis:
	counter_start()
	counter_read()
	counter_set_alarm()
	counter_get_pending_int()

Change-Id: Iaac9a224372ee1c1dd12a223ca222f4485957575
Signed-off-by: Qiu Peiyang <peiyangx.qiu@intel.com>
diff --git a/tests/drivers/aon_counter/README.txt b/tests/drivers/aon_counter/README.txt
deleted file mode 100644
index 8cc3eef..0000000
--- a/tests/drivers/aon_counter/README.txt
+++ /dev/null
@@ -1,92 +0,0 @@
-Title: Quark Always-on counter and timer test app
-
-Description:
-
-A simple test app to test the AON counter and timer for quark d2000 and Quark SE.
-
---------------------------------------------------------------------------------
-
-Building and Running Project:
-
-    make BOARD=quark_d2000_crb
-
---------------------------------------------------------------------------------
-Sample Output:
-
-Always-on counter example app
-Always-on counter started
-Always-on counter value: 125
-Always-on counter value: 218
-Always-on counter value: 310
-Always-on counter value: 401
-Always-on counter value: 493
-Always-on counter value: 586
-Always-on counter value: 678
-Always-on counter value: 769
-Always-on counter value: 862
-Always-on counter value: 952
-Always-on counter value: 1045
-Always-on counter value: 1139
-Always-on counter value: 1233
-Always-on counter value: 1329
-Always-on counter value: 1422
-Always-on counter value: 1518
-Always-on counter value: 1612
-Always-on counter value: 1708
-Always-on counter value: 1802
-Always-on counter value: 1898
-Always-on counter does not support alarm!
-Always-on counter stopped
-Periodic timer example app
-Periodic timer started
-Periodic timer value: ffffff92
-Periodic timer value: ffffff2f
-Periodic timer value: fffffecd
-Periodic timer value: fffffe6a
-Periodic timer value: fffffe09
-Periodic timer value: fffffda6
-Periodic timer value: fffffd45
-Periodic timer value: fffffce4
-Periodic timer value: fffffc83
-Periodic timer value: fffffc22
-Periodic timer value: fffffbc1
-Periodic timer value: fffffb5f
-Periodic timer value: fffffafc
-Periodic timer value: fffffa9a
-Periodic timer value: fffffa37
-Periodic timer value: fffff9d6
-Periodic timer value: fffff973
-Periodic timer value: fffff910
-Periodic timer value: fffff8ae
-Periodic timer value: fffff84d
-Periodic Timer alarm on
-Periodic timer callback data 30
-Periodic timer callback value 9905
-Periodic timer callback data 30
-Periodic timer callback value 9905
-Periodic timer callback data 30
-Periodic timer callback value 9903
-Periodic timer callback data 30
-Periodic timer callback value 9903
-Periodic timer alarm off
-Periodic timer value: 000026c4
-Periodic timer value: 00002661
-Periodic timer value: 000025fe
-Periodic timer value: 0000259b
-Periodic timer value: 00002538
-Periodic timer value: 000024d5
-Periodic timer value: 00002472
-Periodic timer value: 0000240f
-Periodic timer value: 000023ac
-Periodic timer value: 00002348
-Periodic timer value: 000022e5
-Periodic timer value: 00002282
-Periodic timer value: 0000221f
-Periodic timer value: 000021bd
-Periodic timer value: 0000215a
-Periodic timer value: 000020f7
-Periodic timer value: 00002094
-Periodic timer value: 00002032
-Periodic timer value: 00001fcf
-Periodic timer value: 00001f6c
-Periodic timer stopped
diff --git a/tests/drivers/aon_counter/Makefile b/tests/drivers/aon_counter/aon_api/Makefile
similarity index 64%
rename from tests/drivers/aon_counter/Makefile
rename to tests/drivers/aon_counter/aon_api/Makefile
index 20dc0fa..0e0de3a 100644
--- a/tests/drivers/aon_counter/Makefile
+++ b/tests/drivers/aon_counter/aon_api/Makefile
@@ -1,4 +1,4 @@
-BOARD ?= quark_d2000_crb
+BOARD ?= quark_se_c1000_devboard
 CONF_FILE = prj.conf
 
 include ${ZEPHYR_BASE}/Makefile.test
diff --git a/tests/drivers/aon_counter/prj.conf b/tests/drivers/aon_counter/aon_api/prj.conf
similarity index 66%
rename from tests/drivers/aon_counter/prj.conf
rename to tests/drivers/aon_counter/aon_api/prj.conf
index 3caa2e5..e99f557 100644
--- a/tests/drivers/aon_counter/prj.conf
+++ b/tests/drivers/aon_counter/aon_api/prj.conf
@@ -1,3 +1,2 @@
-CONFIG_PRINTK=y
 CONFIG_COUNTER=y
 CONFIG_ZTEST=y
diff --git a/tests/drivers/aon_counter/aon_api/src/Makefile b/tests/drivers/aon_counter/aon_api/src/Makefile
new file mode 100644
index 0000000..c2abc7a
--- /dev/null
+++ b/tests/drivers/aon_counter/aon_api/src/Makefile
@@ -0,0 +1,3 @@
+include $(ZEPHYR_BASE)/tests/Makefile.test
+
+obj-y += main.o test_aon_counter.o test_aon_periodic_timer.o
diff --git a/tests/drivers/aon_counter/aon_api/src/main.c b/tests/drivers/aon_counter/aon_api/src/main.c
new file mode 100644
index 0000000..a1e6190
--- /dev/null
+++ b/tests/drivers/aon_counter/aon_api/src/main.c
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2016 Intel Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/**
+ * @addtogroup t_driver_aon
+ * @{
+ * @defgroup t_aon_basic_operations test_aon_basic_operations
+ * @}
+ */
+
+#include <test_aon.h>
+
+void test_main(void)
+{
+	ztest_test_suite(aon_basic_test,
+			 ztest_unit_test(test_aon_counter),
+			 ztest_unit_test(test_aon_periodic_timer));
+	ztest_run_test_suite(aon_basic_test);
+}
diff --git a/tests/drivers/aon_counter/aon_api/src/test_aon.h b/tests/drivers/aon_counter/aon_api/src/test_aon.h
new file mode 100644
index 0000000..9ffdfcf
--- /dev/null
+++ b/tests/drivers/aon_counter/aon_api/src/test_aon.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2016 Intel Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/**
+ * @file
+ * @brief AON cases header file
+ *
+ * Header file for AON cases
+ */
+
+#ifndef __TEST_AON_H_
+#define __TEST_AON_H__
+
+#include <counter.h>
+#include <zephyr.h>
+#include <ztest.h>
+
+#define AON_COUNTER CONFIG_AON_COUNTER_QMSI_DEV_NAME
+#define AON_TIMER CONFIG_AON_TIMER_QMSI_DEV_NAME
+
+void test_aon_counter(void);
+void test_aon_periodic_timer(void);
+
+#endif /* __TEST_AON_H__ */
diff --git a/tests/drivers/aon_counter/aon_api/src/test_aon_counter.c b/tests/drivers/aon_counter/aon_api/src/test_aon_counter.c
new file mode 100644
index 0000000..d60d5ce
--- /dev/null
+++ b/tests/drivers/aon_counter/aon_api/src/test_aon_counter.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2016 Intel Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/*
+ * @addtogroup test_aon_basic_operations
+ * @{
+ * @defgroup t_aon_counter test_aon_counter
+ * @brief TestPurpose: verify AON counter works well
+ * @details
+ * - Test Steps
+ *   -# Start AON counter and wait for the register change to propagate.
+ *   -# Read the counter value before sleep for a while.
+ *   -# Sleep for 10ms and read the counter value again.
+ *   -# Compare the two counter values.
+ * - Expected Results
+ *   -# AON counter runs at 32768Hz, which means the counter will decrease
+ *	32768 in one second. While k_sleep(10) will sleep 10ms.
+ *
+ *      The expected result is that 100 * counter_delta >= 32768.
+ * @}
+ */
+
+#include <test_aon.h>
+
+static int test_counter(void)
+{
+	uint32_t i = 0;
+	uint32_t p_val, c_val, delta;
+	struct device *aon_counter = device_get_binding(AON_COUNTER);
+
+	if (!aon_counter) {
+		TC_PRINT("Cannot get AON Counter device\n");
+		return TC_FAIL;
+	}
+
+	/* verify counter_start() */
+	if (counter_start(aon_counter)) {
+		TC_PRINT("Fail to start AON Counter device\n");
+		return TC_FAIL;
+	}
+
+	/*
+	 * The AON counter runs from the RTC clock at 32KHz (rather than
+	 * the system clock which is 32MHz) so we need to spin for a few cycles
+	 * allow the register change to propagate.
+	 */
+	k_sleep(10);
+	TC_PRINT("Always-on counter started\n");
+
+	/* verify counter_read() */
+	for (i = 0; i < 20; i++) {
+		p_val = counter_read(aon_counter);
+		k_sleep(10); //sleep 10 ms
+		c_val = counter_read(aon_counter);
+		delta = c_val - p_val;
+		TC_PRINT("Counter values: %u, %u (%u)\n", p_val, c_val, delta);
+
+		if (delta * 100 < 32768) {
+			TC_PRINT("Counter device fails to work\n");
+			return TC_FAIL;
+		}
+	}
+	/*
+	 * arduino_101 loader assumes the counter is running.
+	 * If the counter is stopped, the next app you flash in
+	 * cannot start without a hard reset or power cycle.
+	 * So let's leave the counter in running state.
+	 */
+	return TC_PASS;
+}
+
+void test_aon_counter(void)
+{
+	assert_true(test_counter() == TC_PASS, NULL);
+}
diff --git a/tests/drivers/aon_counter/aon_api/src/test_aon_periodic_timer.c b/tests/drivers/aon_counter/aon_api/src/test_aon_periodic_timer.c
new file mode 100644
index 0000000..065a32a
--- /dev/null
+++ b/tests/drivers/aon_counter/aon_api/src/test_aon_periodic_timer.c
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2016 Intel Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/*
+ * @addtogroup test_aon_basic_operations
+ * @{
+ * @defgroup t_aon_periodic_timer test_aon_periodic_timer
+ * @brief TestPurpose: verify AON timer works well
+ * @details
+ * - Test Steps
+ *   -# Start AON timer and wait for the register change to propagate.
+ *   -# Set timer to alarm every second.
+ *   -# Sleep for a long time to wait for the alarm invoked.
+ * - Expected Results
+ *   -# AON counter runs at 32768Hz, which means the counter will decrease
+ *	32768 in one second. Set AONT down counter initial value register to
+ *	32768, so the alarm will be invoked every second.
+ *
+ *	Sleep for a little longer than 3 seconds, the timer ISR is expected
+ *	to be invoked 3 times.
+ * @}
+ */
+
+#include <test_aon.h>
+
+#define ALARM_CNT 32768 /* about 1s */
+#define SLEEP_TIME 3050 /* a little longer than 3s */
+
+static volatile int actual_alarm_cnt;
+
+static void aon_timer_callback(struct device *dev, void *user_data)
+{
+	TC_PRINT("Periodic timer callback invoked: %u\n", counter_read(dev));
+
+	/* verify counter_get_pending_int() */
+	if (counter_get_pending_int(dev)) {
+		TC_PRINT("Counter interrupt is pending\n");
+		actual_alarm_cnt++;
+	}
+}
+
+static int test_timer(void)
+{
+	uint32_t dummy_data = 30;
+	int expected_alarm_cnt = 0;
+	struct device *aon_timer = device_get_binding(AON_TIMER);
+
+	if (!aon_timer) {
+		TC_PRINT("Cannot get AON Timer device\n");
+		return TC_FAIL;
+	}
+
+	/* verify counter_start() */
+	if (counter_start(aon_timer)) {
+		TC_PRINT("Fail to start AON Timer device\n");
+		return TC_FAIL;
+	}
+
+	/*
+	 * The AON counter runs from the RTC clock at 32KHz (rather than
+	 * the system clock which is 32MHz) so we need to spin for a few cycles
+	 * allow the register change to propagate.
+	 */
+	k_sleep(10);
+	TC_PRINT("Always-on timer started\n");
+
+	/* verify counter_set_alarm() */
+	if (counter_set_alarm(aon_timer, aon_timer_callback,
+			      ALARM_CNT, (void *)&dummy_data)) {
+		TC_PRINT("Fail to set alarm for AON Timer\n");
+		return TC_FAIL;
+	}
+
+	/* long delay for the alarm and callback to happen */
+	k_sleep(SLEEP_TIME);
+
+	if (counter_set_alarm(aon_timer, NULL, 0, NULL)) {
+		TC_PRINT("Periodic timer can not turn off\n");
+		return TC_FAIL;
+	}
+
+	expected_alarm_cnt = (SLEEP_TIME/1000) / (32768/ALARM_CNT);
+
+	TC_PRINT("expected_alarm_cnt = %d\n", expected_alarm_cnt);
+	TC_PRINT("actual_alarm_cnt = %d\n", actual_alarm_cnt);
+
+	if (actual_alarm_cnt != expected_alarm_cnt) {
+		TC_PRINT("actual_alarm_cnt doesn't match expected_alarm_cnt\n");
+		return TC_FAIL;
+	}
+	/*
+	 * arduino_101 loader assumes the counter is running.
+	 * If the counter is stopped, the next app you flash in
+	 * cannot start without a hard reset or power cycle.
+	 * So let's leave the counter in running state.
+	 */
+	return TC_PASS;
+}
+
+void test_aon_periodic_timer(void)
+{
+	assert_true(test_timer() == TC_PASS, NULL);
+}
diff --git a/tests/drivers/aon_counter/aon_api/testcase.ini b/tests/drivers/aon_counter/aon_api/testcase.ini
new file mode 100644
index 0000000..d5c3dd2
--- /dev/null
+++ b/tests/drivers/aon_counter/aon_api/testcase.ini
@@ -0,0 +1,3 @@
+[test_aon]
+tags = drivers
+platform_whitelist = quark_se_c1000_devboard quark_se_c1000_ss_devboard quark_d2000_crb arduino_101 arduino_101_sss
diff --git a/tests/drivers/aon_counter/src/Makefile b/tests/drivers/aon_counter/src/Makefile
deleted file mode 100644
index ef2413b..0000000
--- a/tests/drivers/aon_counter/src/Makefile
+++ /dev/null
@@ -1,3 +0,0 @@
-include $(ZEPHYR_BASE)/tests/Makefile.test
-
-obj-y = main.o
diff --git a/tests/drivers/aon_counter/src/main.c b/tests/drivers/aon_counter/src/main.c
deleted file mode 100644
index 5206051..0000000
--- a/tests/drivers/aon_counter/src/main.c
+++ /dev/null
@@ -1,168 +0,0 @@
-/*
- * Copyright (c) 2016 Intel Corporation
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-#include <ztest.h>
-
-#include <misc/printk.h>
-
-#include <device.h>
-#include <counter.h>
-
-
-/*
- * 0 if not called, != 0 is the value of the counter
- *
- * Note to avoid 0 being posted there because the counter reached 0,
- * we'll add 1 if this is the case.
- */
-static volatile uint32_t aonpt_example_callback_was_called;
-
-static void aonpt_example_callback(struct device *dev, void *user_data)
-{
-	uint32_t counter;
-
-	printk("Periodic timer callback data %p\n", user_data);
-	counter = counter_read(dev);
-	printk("Periodic timer callback value %u\n", counter);
-	aonpt_example_callback_was_called = counter == 0 ? 1 : counter;
-}
-
-static void free_running_counter_example(void)
-{
-	int r;
-	volatile uint32_t delay = 0;
-	unsigned pt_loops = 20, i;
-	uint32_t c_vals[pt_loops + 1];
-
-	struct device *aon_counter_dev;
-
-	printk("Always-on free running counter example app\n");
-
-	aon_counter_dev = device_get_binding("AON_COUNTER");
-	assert_not_null(aon_counter_dev, "Counter device not found\n");
-
-	r = counter_start(aon_counter_dev);
-	assert_equal(r, 0, "Counter device enable didn't return 0\n");
-
-	/*
-	 * The AON counter runs from the RTC clock at 32KHz (rather than
-	 * the system clock which is 32MHz) so we need to spin for a few cycles
-	 * allow the register change to propagate.
-	 */
-	c_vals[0] = counter_read(aon_counter_dev);
-	for (delay = 5000; delay--;) {
-	}
-	c_vals[1] = counter_read(aon_counter_dev);
-	printk("Always-on counter before 5k empty loop %u / after %u\n",
-	       c_vals[0], c_vals[1]);
-	assert_true(c_vals[1] > c_vals[0],
-		    "Always-on counter failed to increase during 5k loop");
-
-	c_vals[0] = counter_read(aon_counter_dev);
-	for (i = 1; i <= pt_loops; i++) {	/* note the i + 1 */
-		for (delay = 500; delay--;) {
-		}
-		c_vals[i] = counter_read(aon_counter_dev);
-		printk("Always-on counter before 500 empty loop %u / after %u\n",
-		       c_vals[i - 1], c_vals[i]);
-		assert_true(c_vals[i] > c_vals[i - 1],
-			    "Always-on counter failed to increase "
-			    "during 500 loop");
-	}
-
-	/*
-	 * arduino 101 loader assumes the counter is running.
-	 * If the counter is stopped, the next app you flash in
-	 * can not start without a hard reset or power cycle.
-	 * Let's leave the counter in running state.
-	 */
-}
-
-static void periodic_timer_example(void)
-{
-	int r;
-	volatile uint32_t delay = 0;
-	uint32_t i = 0;
-	uint32_t dummy_data = 30;
-	uint32_t timer_initial_value = 10000;
-	const unsigned pt_loops = 20;
-	uint32_t pt_val0, pt_vals[pt_loops + 1];
-	struct device *aon_periodic_timer_dev = NULL;
-
-	printk("Periodic timer example app\n");
-	aon_periodic_timer_dev = device_get_binding("AON_TIMER");
-	assert_not_null(aon_periodic_timer_dev, "Timer device not found\n");
-
-	counter_start(aon_periodic_timer_dev);
-	printk("Periodic timer started\n");
-
-	/*
-	 * The AON timer runs from the RTC clock at 32KHz (rather than
-	 * the system clock which is 32MHz) so we need to spin for a few cycles
-	 * allow the register change to propagate.
-	 *
-	 * Note it counts down!
-	 */
-	pt_val0 = counter_read(aon_periodic_timer_dev);
-	for (delay = 5000; delay--;) {
-	}
-	pt_vals[0] = counter_read(aon_periodic_timer_dev);
-	printk("Periodic timer value before 5k %u, after %u\n",
-	       pt_val0, pt_vals[0]);
-	assert_true(pt_vals[0] < pt_val0,
-		    "timer failed to decrease in 5k empty loop");
-	for (i = 1; i < pt_loops + 1; i++) {	/* note the +1 */
-		for (delay = 500; delay--;) {
-		}
-		pt_vals[i] = counter_read(aon_periodic_timer_dev);
-		printk("Periodic timer value before 500 %u, after %u\n",
-		       pt_vals[i - 1], pt_vals[i]);
-		assert_true(pt_vals[i] < pt_vals[i - 1],
-			    "timer failed to decrease in 500 empty loop");
-	}
-
-	r = counter_set_alarm(aon_periodic_timer_dev, aonpt_example_callback,
-			      timer_initial_value, (void *)&dummy_data);
-	assert_equal(r, 0, "Periodic Timer was not started yet\n");
-
-	printk("Periodic Timer alarm on\n");
-
-	/* long delay for the alarm and callback to happen */
-	for (delay = 5000000; delay--;) {
-	}
-	assert_not_equal(aonpt_example_callback_was_called, 0,
-			 "alarm callback was not called");
-	printk("Alarm callback was called with counter %u\n",
-		aonpt_example_callback_was_called);
-
-	/* callback is turned off */
-	r = counter_set_alarm(aon_periodic_timer_dev, NULL,
-			      timer_initial_value, (void *)&dummy_data);
-	assert_equal(r, 0, "Periodic timer was not started yet\n");
-
-	printk("Periodic timer alarm off\n");
-
-	pt_vals[0] = counter_read(aon_periodic_timer_dev);
-	for (i = 1; i < pt_loops + 1; i++) {	/* note the +1 */
-		for (delay = 500; delay--;) {
-		}
-		pt_vals[i] = counter_read(aon_periodic_timer_dev);
-		printk("Periodic timer value before 500 %u, after %u\n",
-		       pt_vals[i - 1], pt_vals[i]);
-		assert_true(pt_vals[i] < pt_vals[i - 1],
-			    "timer failed to decrease in 500 empty loop");
-	}
-
-	counter_stop(aon_periodic_timer_dev);
-}
-
-void test_main(void)
-{
-	ztest_test_suite(
-		aon_counter_test,
-		ztest_unit_test(free_running_counter_example),
-		ztest_unit_test(periodic_timer_example));
-	ztest_run_test_suite(aon_counter_test);
-}
diff --git a/tests/drivers/aon_counter/testcase.ini b/tests/drivers/aon_counter/testcase.ini
deleted file mode 100644
index a31c198..0000000
--- a/tests/drivers/aon_counter/testcase.ini
+++ /dev/null
@@ -1,3 +0,0 @@
-[test]
-tags = apps
-platform_whitelist = quark_d2000_crb quark_se_c1000_devboard quark_se_c1000_ss_devboard arduino_101_sss arduino_101