Debian Codegraph @

test/connect_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2017-2020,2025 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 import os
17 import sys
18 import traceback
19 import argparse
20 from collections import namedtuple
21 import logging
22
23 from pyocd.core.helpers import ConnectHelper
24 from pyocd.core.target import Target
25 from pyocd.flash.file_programmer import FileProgrammer
26
27 from test_util import (
28 Test,
29 TestResult,
30 get_session_options,
31 get_test_binary_path,
32 )
33
34 STATE_NAMES = {
35 Target.State.RUNNING : "running",
36 Target.State.HALTED : "halted",
37 Target.State.RESET : "reset",
38 Target.State.SLEEPING : "sleeping",
39 Target.State.LOCKUP : "lockup",
40 }
41
42 RUNNING = Target.State.RUNNING
43 HALTED = Target.State.HALTED
44
45 class ConnectTestCase(object):
46 def __init__(self, prev_exit_state, connect_mode, expected_state, disconnect_resume, exit_state):
47 self.prev_exit_state = prev_exit_state
48 self.connect_mode = connect_mode
49 self.expected_state = expected_state
50 self.disconnect_resume = disconnect_resume
51 self.exit_state = exit_state
52
53 class ConnectTestResult(TestResult):
54 def __init__(self):
55 super(ConnectTestResult, self).__init__(None, None, None)
56 self.name = "connect"
57
58 class ConnectTest(Test):
59 def __init__(self):
60 super(ConnectTest, self).__init__("Connect Test", connect_test)
61
62 def run(self, board):
63 try:
64 result = self.test_function(board)
65 except Exception as e:
66 print("Exception %s when testing board %s" % (e, board.unique_id))
67 result = ConnectTestResult()
68 result.passed = False
69 traceback.print_exc(file=sys.stdout)
70 result.board = board
71 result.test = self
72 return result
73
74
75 def connect_test(board):
76 board_id = board.unique_id
77 binary_file = get_test_binary_path(board.test_binary)
78 print("binary file: %s" % binary_file)
79
80 test_pass_count = 0
81 test_count = 0
82 result = ConnectTestResult()
83
84 # Install binary.
85 live_session = ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options())
86 live_session.open()
87 live_board = live_session.board
88 memory_map = board.target.get_memory_map()
89 rom_region = memory_map.get_boot_memory()
90 rom_start = rom_region.start
91
92 def test_connect(connect_mode, expected_state, resume):
93 print("Connecting with connect_mode=%s" % connect_mode)
94 live_session = ConnectHelper.session_with_chosen_probe(
95 unique_id=board_id,
96 init_board=False,
97 connect_mode=connect_mode,
98 resume_on_disconnect=resume,
99 **get_session_options())
100 live_session.open()
101 live_board = live_session.board
102 print("Verifying target is", STATE_NAMES.get(expected_state, "unknown"))
103 actualState = live_board.target.get_state()
104 # Accept sleeping for running, as a hack to work around nRF52840-DK test binary.
105 # TODO remove sleeping hack.
106 if (actualState == expected_state) \
107 or (expected_state == RUNNING and actualState == Target.State.SLEEPING):
108 passed = 1
109 print("TEST PASSED")
110 else:
111 passed = 0
112 print("TEST FAILED (state={}, expected={})".format(
113 STATE_NAMES.get(actualState, "unknown"),
114 STATE_NAMES.get(expected_state, "unknown")))
115 print("Disconnecting with resume=%s" % resume)
116 live_session.close()
117 live_session = None
118 return passed
119
120 # TEST CASE COMBINATIONS
121 test_cases = [
122 # <prev_exit> <connect_mode> <expected_state> <disconnect_resume> <exit_state>
123 ConnectTestCase( RUNNING, 'attach', RUNNING, False, RUNNING ),
124 ConnectTestCase( RUNNING, 'halt', HALTED, False, HALTED ),
125 ConnectTestCase( HALTED, 'halt', HALTED, True, RUNNING ),
126 ConnectTestCase( RUNNING, 'halt', HALTED, True, RUNNING ),
127 ConnectTestCase( RUNNING, 'attach', RUNNING, True, RUNNING ),
128 ConnectTestCase( RUNNING, 'halt', HALTED, False, HALTED ),
129 ConnectTestCase( HALTED, 'attach', HALTED, False, HALTED ),
130 ConnectTestCase( HALTED, 'halt', HALTED, False, HALTED ),
131 ConnectTestCase( HALTED, 'attach', HALTED, True, RUNNING ),
132 ConnectTestCase( RUNNING, 'attach', RUNNING, False, RUNNING ),
133 ]
134
135 print("\n\n----- TESTING CONNECT/DISCONNECT -----")
136 print("Flashing new binary")
137 live_board.target.reset_and_halt()
138 FileProgrammer(live_session).program(binary_file, base_address=rom_start)
139 live_board.target.reset()
140 test_count += 1
141 print("Verifying target is running")
142 current_state = live_board.target.get_state()
143 if live_board.target.is_running() or current_state == Target.State.SLEEPING:
144 test_pass_count += 1
145 print("TEST PASSED")
146 else:
147 print("State=%s" % current_state)
148 print("TEST FAILED")
149 print("Disconnecting with resume=True")
150 live_session.options['resume_on_disconnect'] = True
151 live_session.close()
152 live_session = None
153 # Leave running.
154
155 # Run all the cases.
156 for case in test_cases:
157 test_count += 1
158 did_pass = test_connect(
159 connect_mode=case.connect_mode,
160 expected_state=case.expected_state,
161 resume=case.disconnect_resume
162 )
163 test_pass_count += did_pass
164 case.passed=did_pass
165
166 print("\n\nTest Summary:")
167 print("\n{:<4}{:<12}{:<19}{:<12}{:<21}{:<11}{:<10}".format(
168 "#", "Prev Exit", "Connect Mode", "Expected", "Disconnect Resume", "Exit", "Passed"))
169 for i, case in enumerate(test_cases):
170 print("{:<4}{:<12}{:<19}{:<12}{:<21}{:<11}{:<10}".format(
171 i,
172 STATE_NAMES[case.prev_exit_state],
173 case.connect_mode,
174 STATE_NAMES[case.expected_state],
175 repr(case.disconnect_resume),
176 STATE_NAMES[case.exit_state],
177 "PASS" if case.passed else "FAIL"))
178 print("\nPass count %i of %i tests" % (test_pass_count, test_count))
179 if test_pass_count == test_count:
180 print("CONNECT TEST SCRIPT PASSED")
181 else:
182 print("CONNECT TEST SCRIPT FAILED")
183
184 result.passed = (test_count == test_pass_count)
185 return result
186
187 if __name__ == "__main__":
188 parser = argparse.ArgumentParser(description='pyOCD connect test')
189 parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
190 args = parser.parse_args()
191 level = logging.DEBUG if args.debug else logging.INFO
192 logging.basicConfig(level=level)
193 session = ConnectHelper.session_with_chosen_probe(**get_session_options())
194 test = ConnectTest()
195 result = [test.run(session.board)]
196 test.print_perf_info(result)