1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/* Copyright 2022 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "ec_commands.h"
#include "host_command.h"
#include "lpc.h"
#include "system.h"
#include "test/drivers/test_state.h"
#include <zephyr/drivers/i2c_emul.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/ztest_assert.h>
ZTEST_USER(rtc_shim, test_hc_rtc_set_get_value)
{
struct ec_params_rtc set_value;
struct ec_response_rtc get_value;
struct host_cmd_handler_args set_args =
BUILD_HOST_COMMAND_PARAMS(EC_CMD_RTC_SET_VALUE, 0, set_value);
struct host_cmd_handler_args get_args =
BUILD_HOST_COMMAND_RESPONSE(EC_CMD_RTC_GET_VALUE, 0, get_value);
/* Initially set/get arbitrary value */
set_value.time = 1337;
zassert_ok(host_command_process(&set_args));
zassert_ok(host_command_process(&get_args));
zassert_equal(get_value.time, set_value.time);
/* One more time to be sure the test is creating the value change */
set_value.time = 1776;
zassert_ok(host_command_process(&set_args));
zassert_ok(host_command_process(&get_args));
zassert_equal(get_value.time, set_value.time);
}
ZTEST_USER(rtc_shim, test_hc_rtc_set_get_alarm)
{
struct ec_params_rtc set_value;
struct ec_response_rtc get_value;
struct host_cmd_handler_args set_args =
BUILD_HOST_COMMAND_PARAMS(EC_CMD_RTC_SET_ALARM, 0, set_value);
struct host_cmd_handler_args get_args =
BUILD_HOST_COMMAND_RESPONSE(EC_CMD_RTC_GET_ALARM, 0, get_value);
/* Initially set/get zero value */
set_value.time = 0;
zassert_ok(host_command_process(&set_args));
zassert_ok(host_command_process(&get_args));
zassert_equal(get_value.time, set_value.time);
/* One more time to be sure the test is creating the value change */
set_value.time = 1776;
zassert_ok(host_command_process(&set_args));
zassert_ok(host_command_process(&get_args));
/*
* The RTC driver adds 1 second to the alarm time to compensate for
* truncation error. For example, 7 seconds is returned when the
* actual time is 7.9 seconds. "get_value.time - 1" removes the
* additional seconded for this test.
*/
zassert_equal(get_value.time - 1, set_value.time);
}
ZTEST(rtc_shim, test_hc_rtc_set_alarm_can_fire_cb)
{
struct ec_params_rtc set_value;
struct host_cmd_handler_args set_args =
BUILD_HOST_COMMAND_PARAMS(EC_CMD_RTC_SET_ALARM, 0, set_value);
#ifdef CONFIG_HOSTCMD_X86
/* Enable the RTC event to fire */
host_event_t lpc_event_mask;
host_event_t mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC);
lpc_event_mask = lpc_get_host_event_mask(LPC_HOST_EVENT_SMI);
lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, lpc_event_mask | mask);
#endif
/* Arbitrary value of 2 */
system_set_rtc(2);
/* Clear events so we can check that RTC event happened */
host_clear_events(CONFIG_HOST_EVENT_REPORT_MASK);
zassert_false(host_is_event_set(EC_HOST_EVENT_RTC));
/* Initially set arbitrary value of alarm in 2 seconds*/
set_value.time = 2;
zassert_ok(host_command_process(&set_args));
/* Set fake driver time forward to hit the alarm in 2 seconds */
/*
* The RTC driver adds 1 second to the alarm time to compensate for
* truncation error. For example, 7 seconds is returned when the
* actual time is 7.9 seconds. So, RTC seconds is set to 5 instead
* of 4.
*/
system_set_rtc(5);
/* Wait for irq to finish */
k_sleep(K_SECONDS(1));
zassert_true(host_is_event_set(EC_HOST_EVENT_RTC));
}
ZTEST_SUITE(rtc_shim, drivers_predicate_post_main, NULL, NULL, NULL, NULL);
|