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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Test USB PE module.
*/
#include "battery.h"
#include "common.h"
#include "task.h"
#include "test_util.h"
#include "timer.h"
#include "usb_emsg.h"
#include "usb_mux.h"
#include "usb_pe.h"
#include "usb_pe_sm.h"
#include "usb_prl_sm.h"
#include "usb_sm_checks.h"
#include "usb_tc_sm.h"
/**
* STUB Section
*/
const struct svdm_response svdm_rsp = {
.identity = NULL,
.svids = NULL,
.modes = NULL,
};
const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_MAX_COUNT];
const struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT];
int board_vbus_source_enabled(int port)
{
return 0;
}
void tc_request_power_swap(int port)
{
/* Do nothing */
}
void pd_set_vbus_discharge(int port, int enable)
{
gpio_set_level(GPIO_USB_C0_DISCHARGE, enable);
}
uint8_t tc_get_pd_enabled(int port)
{
return 1;
}
bool pd_alt_mode_capable(int port)
{
return 1;
}
/**
* Test section
*/
/* PE Fast Role Swap */
static int test_pe_frs(void)
{
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(pe_is_running(PORT0));
/*
* FRS will only trigger when we are SNK, with an Explicit
* contract. So set this state up manually. Also ensure any
* background tasks (ex. discovery) aren't running.
*/
tc_prs_src_snk_assert_rd(PORT0);
pe_set_flag(PORT0, PE_FLAGS_DISCOVER_PORT_IDENTITY_DONE);
pe_set_flag(PORT0, PE_FLAGS_EXPLICIT_CONTRACT);
set_state_pe(PORT0, PE_SNK_READY);
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(get_state_pe(PORT0) == PE_SNK_READY);
/*
* Trigger the Fast Role Switch from simulated ISR
*/
pd_got_frs_signal(PORT0);
TEST_ASSERT(pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_SIGNALED));
/*
* Verify we detected FRS and ready to start swap
*/
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(get_state_pe(PORT0) == PE_PRS_SNK_SRC_SEND_SWAP);
TEST_ASSERT(pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_PATH));
TEST_ASSERT(!pe_chk_flag(PORT0, PE_FLAGS_EXPLICIT_CONTRACT));
/*
* Make sure that we sent FR_Swap
*/
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(fake_prl_get_last_sent_ctrl_msg(PORT0) == PD_CTRL_FR_SWAP);
TEST_ASSERT(get_state_pe(PORT0) == PE_PRS_SNK_SRC_SEND_SWAP);
TEST_ASSERT(pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_PATH));
/*
* Accept the partners PS_RDY control message
*/
rx_emsg[PORT0].header = PD_HEADER(PD_CTRL_ACCEPT, 0, 0, 0, 0, 0, 0);
pe_set_flag(PORT0, PE_FLAGS_MSG_RECEIVED);
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(!pe_chk_flag(PORT0, PE_FLAGS_MSG_RECEIVED));
TEST_ASSERT(get_state_pe(PORT0) == PE_PRS_SNK_SRC_TRANSITION_TO_OFF);
TEST_ASSERT(pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_PATH));
/*
* Send back our PS_RDY
*/
rx_emsg[PORT0].header = PD_HEADER(PD_CTRL_PS_RDY, 0, 0, 0, 0, 0, 0);
pe_set_flag(PORT0, PE_FLAGS_MSG_RECEIVED);
TEST_ASSERT(!tc_is_attached_src(PORT0));
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(!pe_chk_flag(PORT0, PE_FLAGS_MSG_RECEIVED));
TEST_ASSERT(tc_is_attached_src(PORT0));
TEST_ASSERT(get_state_pe(PORT0) == PE_PRS_SNK_SRC_ASSERT_RP);
TEST_ASSERT(pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_PATH));
/*
* We are now attached source, so move to next state
*/
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(get_state_pe(PORT0) == PE_PRS_SNK_SRC_SOURCE_ON);
TEST_ASSERT(pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_PATH));
/*
* Move the time to be after our wait time.
*/
force_time((timestamp_t)(get_time().val +
PD_POWER_SUPPLY_TURN_ON_DELAY));
/*
* After delay we are ready to send our PS_RDY
*/
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(get_state_pe(PORT0) == PE_PRS_SNK_SRC_SOURCE_ON);
TEST_ASSERT(pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_PATH));
TEST_ASSERT(fake_prl_get_last_sent_ctrl_msg(PORT0) == PD_CTRL_PS_RDY);
/*
* Fake the Transmit complete and this will bring us to Source Startup
*/
pe_set_flag(PORT0, PE_FLAGS_TX_COMPLETE);
pe_run(PORT0, EVT_IGNORED, ENABLED);
TEST_ASSERT(get_state_pe(PORT0) == PE_SRC_STARTUP);
TEST_ASSERT(!pe_chk_flag(PORT0, PE_FLAGS_FAST_ROLE_SWAP_PATH));
return EC_SUCCESS;
}
static int test_vbus_gpio_discharge(void)
{
pd_set_vbus_discharge(PORT0, 1);
TEST_EQ(gpio_get_level(GPIO_USB_C0_DISCHARGE), 1, "%d");
pd_set_vbus_discharge(PORT0, 0);
TEST_EQ(gpio_get_level(GPIO_USB_C0_DISCHARGE), 0, "%d");
return EC_SUCCESS;
}
void run_test(void)
{
test_reset();
RUN_TEST(test_pe_frs);
RUN_TEST(test_vbus_gpio_discharge);
/* Do basic state machine sanity checks last. */
RUN_TEST(test_pe_no_parent_cycles);
RUN_TEST(test_pe_no_empty_state);
test_print_result();
}
|