Skip to content

Commit 45c579a

Browse files
QiuPeiyangAnas Nashif
authored andcommitted
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 <[email protected]>
1 parent f16f6ec commit 45c579a

File tree

12 files changed

+240
-268
lines changed

12 files changed

+240
-268
lines changed

tests/drivers/aon_counter/README.txt

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
BOARD ?= quark_d2000_crb
1+
BOARD ?= quark_se_c1000_devboard
22
CONF_FILE = prj.conf
33

44
include ${ZEPHYR_BASE}/Makefile.test
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
CONFIG_PRINTK=y
21
CONFIG_COUNTER=y
32
CONFIG_ZTEST=y
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include $(ZEPHYR_BASE)/tests/Makefile.test
2+
3+
obj-y += main.o test_aon_counter.o test_aon_periodic_timer.o
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @addtogroup t_driver_aon
9+
* @{
10+
* @defgroup t_aon_basic_operations test_aon_basic_operations
11+
* @}
12+
*/
13+
14+
#include <test_aon.h>
15+
16+
void test_main(void)
17+
{
18+
ztest_test_suite(aon_basic_test,
19+
ztest_unit_test(test_aon_counter),
20+
ztest_unit_test(test_aon_periodic_timer));
21+
ztest_run_test_suite(aon_basic_test);
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief AON cases header file
10+
*
11+
* Header file for AON cases
12+
*/
13+
14+
#ifndef __TEST_AON_H_
15+
#define __TEST_AON_H__
16+
17+
#include <counter.h>
18+
#include <zephyr.h>
19+
#include <ztest.h>
20+
21+
#define AON_COUNTER CONFIG_AON_COUNTER_QMSI_DEV_NAME
22+
#define AON_TIMER CONFIG_AON_TIMER_QMSI_DEV_NAME
23+
24+
void test_aon_counter(void);
25+
void test_aon_periodic_timer(void);
26+
27+
#endif /* __TEST_AON_H__ */
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* @addtogroup test_aon_basic_operations
9+
* @{
10+
* @defgroup t_aon_counter test_aon_counter
11+
* @brief TestPurpose: verify AON counter works well
12+
* @details
13+
* - Test Steps
14+
* -# Start AON counter and wait for the register change to propagate.
15+
* -# Read the counter value before sleep for a while.
16+
* -# Sleep for 10ms and read the counter value again.
17+
* -# Compare the two counter values.
18+
* - Expected Results
19+
* -# AON counter runs at 32768Hz, which means the counter will decrease
20+
* 32768 in one second. While k_sleep(10) will sleep 10ms.
21+
*
22+
* The expected result is that 100 * counter_delta >= 32768.
23+
* @}
24+
*/
25+
26+
#include <test_aon.h>
27+
28+
static int test_counter(void)
29+
{
30+
uint32_t i = 0;
31+
uint32_t p_val, c_val, delta;
32+
struct device *aon_counter = device_get_binding(AON_COUNTER);
33+
34+
if (!aon_counter) {
35+
TC_PRINT("Cannot get AON Counter device\n");
36+
return TC_FAIL;
37+
}
38+
39+
/* verify counter_start() */
40+
if (counter_start(aon_counter)) {
41+
TC_PRINT("Fail to start AON Counter device\n");
42+
return TC_FAIL;
43+
}
44+
45+
/*
46+
* The AON counter runs from the RTC clock at 32KHz (rather than
47+
* the system clock which is 32MHz) so we need to spin for a few cycles
48+
* allow the register change to propagate.
49+
*/
50+
k_sleep(10);
51+
TC_PRINT("Always-on counter started\n");
52+
53+
/* verify counter_read() */
54+
for (i = 0; i < 20; i++) {
55+
p_val = counter_read(aon_counter);
56+
k_sleep(10); //sleep 10 ms
57+
c_val = counter_read(aon_counter);
58+
delta = c_val - p_val;
59+
TC_PRINT("Counter values: %u, %u (%u)\n", p_val, c_val, delta);
60+
61+
if (delta * 100 < 32768) {
62+
TC_PRINT("Counter device fails to work\n");
63+
return TC_FAIL;
64+
}
65+
}
66+
/*
67+
* arduino_101 loader assumes the counter is running.
68+
* If the counter is stopped, the next app you flash in
69+
* cannot start without a hard reset or power cycle.
70+
* So let's leave the counter in running state.
71+
*/
72+
return TC_PASS;
73+
}
74+
75+
void test_aon_counter(void)
76+
{
77+
assert_true(test_counter() == TC_PASS, NULL);
78+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* @addtogroup test_aon_basic_operations
9+
* @{
10+
* @defgroup t_aon_periodic_timer test_aon_periodic_timer
11+
* @brief TestPurpose: verify AON timer works well
12+
* @details
13+
* - Test Steps
14+
* -# Start AON timer and wait for the register change to propagate.
15+
* -# Set timer to alarm every second.
16+
* -# Sleep for a long time to wait for the alarm invoked.
17+
* - Expected Results
18+
* -# AON counter runs at 32768Hz, which means the counter will decrease
19+
* 32768 in one second. Set AONT down counter initial value register to
20+
* 32768, so the alarm will be invoked every second.
21+
*
22+
* Sleep for a little longer than 3 seconds, the timer ISR is expected
23+
* to be invoked 3 times.
24+
* @}
25+
*/
26+
27+
#include <test_aon.h>
28+
29+
#define ALARM_CNT 32768 /* about 1s */
30+
#define SLEEP_TIME 3050 /* a little longer than 3s */
31+
32+
static volatile int actual_alarm_cnt;
33+
34+
static void aon_timer_callback(struct device *dev, void *user_data)
35+
{
36+
TC_PRINT("Periodic timer callback invoked: %u\n", counter_read(dev));
37+
38+
/* verify counter_get_pending_int() */
39+
if (counter_get_pending_int(dev)) {
40+
TC_PRINT("Counter interrupt is pending\n");
41+
actual_alarm_cnt++;
42+
}
43+
}
44+
45+
static int test_timer(void)
46+
{
47+
uint32_t dummy_data = 30;
48+
int expected_alarm_cnt = 0;
49+
struct device *aon_timer = device_get_binding(AON_TIMER);
50+
51+
if (!aon_timer) {
52+
TC_PRINT("Cannot get AON Timer device\n");
53+
return TC_FAIL;
54+
}
55+
56+
/* verify counter_start() */
57+
if (counter_start(aon_timer)) {
58+
TC_PRINT("Fail to start AON Timer device\n");
59+
return TC_FAIL;
60+
}
61+
62+
/*
63+
* The AON counter runs from the RTC clock at 32KHz (rather than
64+
* the system clock which is 32MHz) so we need to spin for a few cycles
65+
* allow the register change to propagate.
66+
*/
67+
k_sleep(10);
68+
TC_PRINT("Always-on timer started\n");
69+
70+
/* verify counter_set_alarm() */
71+
if (counter_set_alarm(aon_timer, aon_timer_callback,
72+
ALARM_CNT, (void *)&dummy_data)) {
73+
TC_PRINT("Fail to set alarm for AON Timer\n");
74+
return TC_FAIL;
75+
}
76+
77+
/* long delay for the alarm and callback to happen */
78+
k_sleep(SLEEP_TIME);
79+
80+
if (counter_set_alarm(aon_timer, NULL, 0, NULL)) {
81+
TC_PRINT("Periodic timer can not turn off\n");
82+
return TC_FAIL;
83+
}
84+
85+
expected_alarm_cnt = (SLEEP_TIME/1000) / (32768/ALARM_CNT);
86+
87+
TC_PRINT("expected_alarm_cnt = %d\n", expected_alarm_cnt);
88+
TC_PRINT("actual_alarm_cnt = %d\n", actual_alarm_cnt);
89+
90+
if (actual_alarm_cnt != expected_alarm_cnt) {
91+
TC_PRINT("actual_alarm_cnt doesn't match expected_alarm_cnt\n");
92+
return TC_FAIL;
93+
}
94+
/*
95+
* arduino_101 loader assumes the counter is running.
96+
* If the counter is stopped, the next app you flash in
97+
* cannot start without a hard reset or power cycle.
98+
* So let's leave the counter in running state.
99+
*/
100+
return TC_PASS;
101+
}
102+
103+
void test_aon_periodic_timer(void)
104+
{
105+
assert_true(test_timer() == TC_PASS, NULL);
106+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[test_aon]
2+
tags = drivers
3+
platform_whitelist = quark_se_c1000_devboard quark_se_c1000_ss_devboard quark_d2000_crb arduino_101 arduino_101_sss

tests/drivers/aon_counter/src/Makefile

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)