| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import argparse |
| 17 |
import sys |
| 18 |
import traceback |
| 19 |
import logging |
| 20 |
import tempfile |
| 21 |
import platform |
| 22 |
|
| 23 |
from pyocd.core.helpers import ConnectHelper |
| 24 |
from pyocd.probe.pydapaccess import DAPAccess |
| 25 |
from pyocd.utility.mask import round_up_div |
| 26 |
from pyocd.utility import conversion |
| 27 |
from pyocd.core.memory_map import MemoryType |
| 28 |
from pyocd.commands.execution_context import CommandExecutionContext |
| 29 |
from pyocd.commands import commands |
| 30 |
from test_util import ( |
| 31 |
Test, |
| 32 |
TestResult, |
| 33 |
get_session_options, |
| 34 |
get_target_test_params, |
| 35 |
binary_to_hex_file, |
| 36 |
binary_to_elf_file, |
| 37 |
get_test_binary_path, |
| 38 |
) |
| 39 |
|
| 40 |
class CommandsTestResult(TestResult): |
| 41 |
def __init__(self): |
| 42 |
super(CommandsTestResult, self).__init__(None, None, None) |
| 43 |
self.name = "commands" |
| 44 |
|
| 45 |
class CommandsTest(Test): |
| 46 |
def __init__(self): |
| 47 |
super(CommandsTest, self).__init__("Commands Test", commands_test) |
| 48 |
|
| 49 |
def run(self, board): |
| 50 |
try: |
| 51 |
result = self.test_function(board.unique_id) |
| 52 |
except Exception as e: |
| 53 |
result = CommandsTestResult() |
| 54 |
result.passed = False |
| 55 |
print("Exception %s when testing board %s" % (e, board.unique_id)) |
| 56 |
traceback.print_exc(file=sys.stdout) |
| 57 |
result.board = board |
| 58 |
result.test = self |
| 59 |
return result |
| 60 |
|
| 61 |
def fix_windows_path(path: str) -> str: |
| 62 |
"""Double backslashes in paths on Windows.""" |
| 63 |
if platform.system() == "Windows": |
| 64 |
return path.replace('\\', '\\\\') |
| 65 |
else: |
| 66 |
return path |
| 67 |
|
| 68 |
def commands_test(board_id): |
| 69 |
with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session: |
| 70 |
board = session.board |
| 71 |
target = session.target |
| 72 |
target_type = board.target_type |
| 73 |
|
| 74 |
test_params = get_target_test_params(session) |
| 75 |
session.probe.set_clock(test_params['test_clock']) |
| 76 |
|
| 77 |
memory_map = board.target.get_memory_map() |
| 78 |
boot_region = memory_map.get_boot_memory() |
| 79 |
ram_region = memory_map.get_default_region_of_type(MemoryType.RAM) |
| 80 |
ram_base = ram_region.start |
| 81 |
boot_start_addr = boot_region.start |
| 82 |
boot_end_addr = boot_region.end |
| 83 |
boot_blocksize = boot_region.blocksize |
| 84 |
binary_file = get_test_binary_path(board.test_binary) |
| 85 |
|
| 86 |
|
| 87 |
temp_test_hex_name = binary_to_hex_file(binary_file, boot_region.start) |
| 88 |
|
| 89 |
temp_bin_file = tempfile.mktemp('.bin') |
| 90 |
|
| 91 |
with open(binary_file, "rb") as f: |
| 92 |
test_data = list(bytearray(f.read())) |
| 93 |
test_data_length = len(test_data) |
| 94 |
test_file_sectors = round_up_div(test_data_length, boot_blocksize) |
| 95 |
boot_first_free_block_addr = boot_start_addr + test_file_sectors * boot_blocksize |
| 96 |
reset_handler_addr = conversion.byte_list_to_u32le_list(test_data[4:8])[0] |
| 97 |
|
| 98 |
test_pass_count = 0 |
| 99 |
test_count = 0 |
| 100 |
failed_commands = [] |
| 101 |
result = CommandsTestResult() |
| 102 |
|
| 103 |
context = CommandExecutionContext() |
| 104 |
context.attach_session(session) |
| 105 |
|
| 106 |
COMMANDS_TO_TEST = [ |
| 107 |
"status", |
| 108 |
"reset", |
| 109 |
"reset halt", |
| 110 |
"reg", |
| 111 |
"reg general", |
| 112 |
"reg all", |
| 113 |
"reg r0", |
| 114 |
"wreg r0 0x12345678", |
| 115 |
"d pc", |
| 116 |
"d --center pc 32", |
| 117 |
"read64 0x%08x" % ((boot_start_addr + boot_blocksize) & ~7), |
| 118 |
"read32 0x%08x" % ((boot_start_addr + boot_blocksize) & ~3), |
| 119 |
"read16 0x%08x" % ((boot_start_addr + boot_blocksize) & ~1), |
| 120 |
"read8 0x%08x" % (boot_start_addr + boot_blocksize), |
| 121 |
"rd 0x%08x 16" % ram_base, |
| 122 |
"rw 0x%08x 16" % ram_base, |
| 123 |
"rh 0x%08x 16" % ram_base, |
| 124 |
"rb 0x%08x 16" % ram_base, |
| 125 |
"write64 0x%08x 0x1122334455667788 0xaabbccddeeff0011" % ram_base, |
| 126 |
"write32 0x%08x 0x11223344 0x55667788" % ram_base, |
| 127 |
"write16 0x%08x 0xabcd" % (ram_base + 8), |
| 128 |
"write8 0x%08x 0 1 2 3 4 5 6" % (ram_base + 10), |
| 129 |
"savemem 0x%08x 128 '%s'" % (boot_start_addr, fix_windows_path(temp_bin_file)), |
| 130 |
"loadmem 0x%08x '%s'" % (ram_base, fix_windows_path(temp_bin_file)), |
| 131 |
"loadmem 0x%08x '%s'" % (boot_start_addr, fix_windows_path(binary_file)), |
| 132 |
"load '%s'" % fix_windows_path(temp_test_hex_name), |
| 133 |
"load '%s' 0x%08x" % (fix_windows_path(binary_file), boot_start_addr), |
| 134 |
"compare 0x%08x '%s'" % (ram_base, fix_windows_path(temp_bin_file)), |
| 135 |
"compare 0x%08x 32 '%s'" % (ram_base, fix_windows_path(temp_bin_file)), |
| 136 |
"fill 0x%08x 128 0xa5" % ram_base, |
| 137 |
"fill 16 0x%08x 64 0x55aa" % (ram_base + 64), |
| 138 |
"find 0x%08x 128 0xaa 0x55" % ram_base, |
| 139 |
"find 0x%08x 128 0xff" % ram_base, |
| 140 |
"find -n 0x%08x 128 0xff" % ram_base, |
| 141 |
"erase 0x%08x" % (boot_first_free_block_addr), |
| 142 |
"erase 0x%08x 1" % (boot_first_free_block_addr + boot_blocksize), |
| 143 |
"go", |
| 144 |
"halt", |
| 145 |
"step", |
| 146 |
"s 4", |
| 147 |
"continue", |
| 148 |
"h", |
| 149 |
"break 0x%08x" % reset_handler_addr, |
| 150 |
"lsbreak", |
| 151 |
"rmbreak 0x%08x" % reset_handler_addr, |
| 152 |
"watch 0x%08x" % ram_base, |
| 153 |
"lswatch", |
| 154 |
"rmwatch 0x%08x" % ram_base, |
| 155 |
"watch 0x%08x rw 2" % ram_base, |
| 156 |
"rmwatch 0x%08x" % ram_base, |
| 157 |
"core", |
| 158 |
"core 0", |
| 159 |
"readdp 0", |
| 160 |
"writedp 0 0x1e", |
| 161 |
"readap 0xfc", |
| 162 |
"readap 0 0xfc", |
| 163 |
"writeap 0x4 0", |
| 164 |
"writeap 0 0x4 0", |
| 165 |
"gdbserver start", |
| 166 |
"gdbserver status", |
| 167 |
"gdbserver stop", |
| 168 |
"probeserver start", |
| 169 |
"probeserver status", |
| 170 |
"probeserver stop", |
| 171 |
"show probe-uid", |
| 172 |
"show target", |
| 173 |
"show cores", |
| 174 |
"show map", |
| 175 |
"show peripherals", |
| 176 |
"show fault", |
| 177 |
"show nreset", |
| 178 |
"set nreset 1", |
| 179 |
"show option reset_type", |
| 180 |
"set option reset_type=sw", |
| 181 |
"show mem-ap", |
| 182 |
"set mem-ap 0", |
| 183 |
"show hnonsec", |
| 184 |
"set hnonsec 0", |
| 185 |
"show hprot", |
| 186 |
"set hprot 0x3", |
| 187 |
"show graph", |
| 188 |
"show locked", |
| 189 |
"show register-groups", |
| 190 |
"show vector-catch", |
| 191 |
"set vector-catch all", |
| 192 |
"show step-into-interrupts", |
| 193 |
"set step-into-interrupts 1", |
| 194 |
"set log info", |
| 195 |
"set frequency %d" % test_params['test_clock'], |
| 196 |
|
| 197 |
|
| 198 |
'rw 0x%08x ; rw 0x%08x' % (ram_base, ram_base + 4), |
| 199 |
'rb 0x%08x;rb 0x%08x' % (ram_base, ram_base + 1), |
| 200 |
'rb 0x%08x; rb 0x%08x' % (ram_base, ram_base + 1), |
| 201 |
|
| 202 |
|
| 203 |
'$2+ 2', |
| 204 |
'$ target', |
| 205 |
'!echo hello', |
| 206 |
'!echo hi ; echo there', |
| 207 |
' $ " ".join(["hello", "there"])', |
| 208 |
' ! echo "yo dude" ', |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
] |
| 221 |
|
| 222 |
|
| 223 |
print("\n------ Testing commands ------") |
| 224 |
|
| 225 |
def test_command(cmd, print_result=True): |
| 226 |
try: |
| 227 |
print("\nTEST: %s" % cmd) |
| 228 |
context.process_command_line(cmd) |
| 229 |
except Exception: |
| 230 |
if print_result: |
| 231 |
print("TEST FAILED") |
| 232 |
failed_commands.append(cmd) |
| 233 |
traceback.print_exc(file=sys.stdout) |
| 234 |
return False |
| 235 |
else: |
| 236 |
if print_result: |
| 237 |
print("TEST PASSED") |
| 238 |
return True |
| 239 |
|
| 240 |
for cmd in COMMANDS_TO_TEST: |
| 241 |
if test_command(cmd): |
| 242 |
test_pass_count += 1 |
| 243 |
test_count += 1 |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
print("\n------ Additional command tests ------") |
| 249 |
|
| 250 |
|
| 251 |
session.options['auto_unlock'] = False |
| 252 |
if test_command('set option auto_unlock=on', print_result=False): |
| 253 |
if session.options['auto_unlock'] is True: |
| 254 |
test_pass_count += 1 |
| 255 |
print("TEST PASSED") |
| 256 |
else: |
| 257 |
print("TEST FAILED") |
| 258 |
|
| 259 |
test_count += 1 |
| 260 |
|
| 261 |
print("\n\nTest Summary:") |
| 262 |
print("Pass count %i of %i tests" % (test_pass_count, test_count)) |
| 263 |
if failed_commands: |
| 264 |
for c in failed_commands: |
| 265 |
print(" - '" + c + "'") |
| 266 |
if test_pass_count == test_count: |
| 267 |
print("COMMANDS TEST SCRIPT PASSED") |
| 268 |
else: |
| 269 |
print("COMMANDS TEST SCRIPT FAILED") |
| 270 |
|
| 271 |
target.reset() |
| 272 |
|
| 273 |
result.passed = test_count == test_pass_count |
| 274 |
return result |
| 275 |
|
| 276 |
if __name__ == "__main__": |
| 277 |
parser = argparse.ArgumentParser(description='pyOCD commands test') |
| 278 |
parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging') |
| 279 |
parser.add_argument('-u', '--uid', help='Debug probe unique ID') |
| 280 |
parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.") |
| 281 |
args = parser.parse_args() |
| 282 |
level = logging.DEBUG if args.debug else logging.INFO |
| 283 |
logging.basicConfig(level=level) |
| 284 |
DAPAccess.set_args(args.daparg) |
| 285 |
commands_test(args.uid) |