| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import argparse |
| 17 |
import os |
| 18 |
import sys |
| 19 |
from time import (sleep, time) |
| 20 |
from random import randrange |
| 21 |
import math |
| 22 |
import argparse |
| 23 |
import traceback |
| 24 |
import logging |
| 25 |
from random import randrange |
| 26 |
|
| 27 |
from pyocd.core.target import Target |
| 28 |
from pyocd.coresight.cortex_m import CortexM |
| 29 |
from pyocd.gdbserver.context_facade import GDBDebugContextFacade |
| 30 |
from pyocd.core.helpers import ConnectHelper |
| 31 |
from pyocd.utility.conversion import float32_to_u32, u32_to_float32 |
| 32 |
from pyocd.utility.mask import same |
| 33 |
from pyocd.core import exceptions |
| 34 |
from pyocd.core.memory_map import MemoryType |
| 35 |
from pyocd.flash.file_programmer import FileProgrammer |
| 36 |
|
| 37 |
from test_util import ( |
| 38 |
Test, |
| 39 |
TestResult, |
| 40 |
get_session_options, |
| 41 |
get_target_test_params, |
| 42 |
get_test_binary_path, |
| 43 |
) |
| 44 |
|
| 45 |
TEST_COUNT = 20 |
| 46 |
|
| 47 |
class CortexTestResult(TestResult): |
| 48 |
METRICS = [ |
| 49 |
"get_t_response", |
| 50 |
"step", |
| 51 |
"bp_add_remove", |
| 52 |
"get_reg_context", |
| 53 |
"set_reg_context", |
| 54 |
"run_halt", |
| 55 |
"gdb_step", |
| 56 |
] |
| 57 |
|
| 58 |
def __init__(self): |
| 59 |
super(CortexTestResult, self).__init__(None, None, None) |
| 60 |
self.name = "cortex" |
| 61 |
self.times = {} |
| 62 |
for metric in self.METRICS: |
| 63 |
self.times[metric] = -1.0 |
| 64 |
|
| 65 |
class CortexTest(Test): |
| 66 |
def __init__(self): |
| 67 |
super(CortexTest, self).__init__("Cortex Test", cortex_test) |
| 68 |
|
| 69 |
def print_perf_info(self, result_list, output_file=None): |
| 70 |
result_list = filter(lambda x: isinstance(x, CortexTestResult), result_list) |
| 71 |
print("\n\n------ Cortex Test Performance ------", file=output_file) |
| 72 |
print("", file=output_file) |
| 73 |
for result in result_list: |
| 74 |
print("Target {}:".format(result.board), file=output_file) |
| 75 |
for metric in CortexTestResult.METRICS: |
| 76 |
print(" {:<20} = {: 8.3f} ms".format(metric, result.times[metric] * 1000), file=output_file) |
| 77 |
print("", file=output_file) |
| 78 |
|
| 79 |
def run(self, board): |
| 80 |
try: |
| 81 |
result = self.test_function(board.unique_id) |
| 82 |
except Exception as e: |
| 83 |
result = CortexTestResult() |
| 84 |
result.passed = False |
| 85 |
print("Exception %s when testing board %s" % (e, board.unique_id)) |
| 86 |
traceback.print_exc(file=sys.stdout) |
| 87 |
result.board = board |
| 88 |
result.test = self |
| 89 |
return result |
| 90 |
|
| 91 |
def float_compare(f1, f2): |
| 92 |
return abs(f1 - f2) < 0.0001 |
| 93 |
|
| 94 |
def test_function(session, function): |
| 95 |
session.probe.flush() |
| 96 |
start = time() |
| 97 |
for i in range(0, TEST_COUNT): |
| 98 |
function() |
| 99 |
session.probe.flush() |
| 100 |
stop = time() |
| 101 |
return (stop - start) / float(TEST_COUNT) |
| 102 |
|
| 103 |
def cortex_test(board_id): |
| 104 |
with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session: |
| 105 |
board = session.board |
| 106 |
target_type = board.target_type |
| 107 |
|
| 108 |
binary_file = get_test_binary_path(board.test_binary) |
| 109 |
|
| 110 |
test_params = get_target_test_params(session) |
| 111 |
test_clock = test_params['test_clock'] |
| 112 |
addr_invalid = 0x3E000000 |
| 113 |
expect_invalid_access_to_fail = test_params['error_on_invalid_access'] |
| 114 |
|
| 115 |
memory_map = board.target.get_memory_map() |
| 116 |
ram_region = memory_map.get_default_region_of_type(MemoryType.RAM) |
| 117 |
rom_region = memory_map.get_boot_memory() |
| 118 |
|
| 119 |
addr = ram_region.start |
| 120 |
size = 0x502 |
| 121 |
addr_bin = rom_region.start |
| 122 |
|
| 123 |
target = board.target |
| 124 |
probe = session.probe |
| 125 |
|
| 126 |
probe.set_clock(test_clock) |
| 127 |
|
| 128 |
test_pass_count = 0 |
| 129 |
test_count = 0 |
| 130 |
result = CortexTestResult() |
| 131 |
|
| 132 |
debugContext = target.get_target_context() |
| 133 |
gdbFacade = GDBDebugContextFacade(debugContext) |
| 134 |
|
| 135 |
print("\n\n----- FLASH NEW BINARY BEFORE TEST -----") |
| 136 |
target.reset_and_halt() |
| 137 |
FileProgrammer(session).program(binary_file, base_address=addr_bin) |
| 138 |
|
| 139 |
|
| 140 |
target.resume() |
| 141 |
sleep(0.2) |
| 142 |
target.halt() |
| 143 |
|
| 144 |
print("PROGRAMMING COMPLETE") |
| 145 |
|
| 146 |
|
| 147 |
print("\n\n----- TESTING CORTEX-M PERFORMANCE -----") |
| 148 |
test_time = test_function(session, gdbFacade.get_t_response) |
| 149 |
result.times["get_t_response"] = test_time |
| 150 |
print("Function get_t_response time: %f" % test_time) |
| 151 |
|
| 152 |
|
| 153 |
test_time = test_function(session, target.step) |
| 154 |
result.times["step"] = test_time |
| 155 |
print("Function step time: %f" % test_time) |
| 156 |
|
| 157 |
|
| 158 |
def set_remove_breakpoint(): |
| 159 |
target.set_breakpoint(0) |
| 160 |
target.remove_breakpoint(0) |
| 161 |
test_time = test_function(session, set_remove_breakpoint) |
| 162 |
result.times["bp_add_remove"] = test_time |
| 163 |
print("Add and remove breakpoint: %f" % test_time) |
| 164 |
|
| 165 |
|
| 166 |
test_time = test_function(session, gdbFacade.get_register_context) |
| 167 |
result.times["get_reg_context"] = test_time |
| 168 |
print("Function get_register_context: %f" % test_time) |
| 169 |
|
| 170 |
|
| 171 |
context = gdbFacade.get_register_context() |
| 172 |
def set_register_context(): |
| 173 |
gdbFacade.set_register_context(context) |
| 174 |
test_time = test_function(session, set_register_context) |
| 175 |
result.times["set_reg_context"] = test_time |
| 176 |
print("Function set_register_context: %f" % test_time) |
| 177 |
|
| 178 |
|
| 179 |
def run_halt(): |
| 180 |
target.resume() |
| 181 |
target.halt() |
| 182 |
test_time = test_function(session, run_halt) |
| 183 |
result.times["run_halt"] = test_time |
| 184 |
print("Resume and halt: %f" % test_time) |
| 185 |
|
| 186 |
|
| 187 |
def simulate_step(): |
| 188 |
target.step() |
| 189 |
gdbFacade.get_t_response() |
| 190 |
target.set_breakpoint(0) |
| 191 |
target.resume() |
| 192 |
target.halt() |
| 193 |
gdbFacade.get_t_response() |
| 194 |
target.remove_breakpoint(0) |
| 195 |
test_time = test_function(session, simulate_step) |
| 196 |
result.times["gdb_step"] = test_time |
| 197 |
print("Simulated GDB step: %f" % test_time) |
| 198 |
|
| 199 |
|
| 200 |
test_pass_count += 1 |
| 201 |
test_count += 1 |
| 202 |
print("TEST PASSED") |
| 203 |
|
| 204 |
print("\n\n------ Testing Reset Types ------") |
| 205 |
def reset_methods(fnc): |
| 206 |
test_hw_reset = Target.ResetType.NSRST in target.selected_core.supported_reset_types |
| 207 |
test_system_reset = Target.ResetType.SYSRESETREQ in target.selected_core.supported_reset_types |
| 208 |
test_core_reset = Target.ResetType.VECTRESET in target.selected_core.supported_reset_types |
| 209 |
test_emulated_reset = Target.ResetType.EMULATED in target.selected_core.supported_reset_types |
| 210 |
|
| 211 |
if test_hw_reset: |
| 212 |
print("Hardware reset") |
| 213 |
fnc(reset_type=Target.ResetType.NSRST) |
| 214 |
print("Hardware reset (default=NSRST)") |
| 215 |
target.selected_core.default_reset_type = Target.ResetType.NSRST |
| 216 |
fnc(reset_type=None) |
| 217 |
else: |
| 218 |
print("Hardware reset is disabled") |
| 219 |
if test_system_reset: |
| 220 |
print("Software reset (default=SYSRESETREQ)") |
| 221 |
target.selected_core.default_reset_type = Target.ResetType.SYSRESETREQ |
| 222 |
fnc(reset_type=None) |
| 223 |
else: |
| 224 |
print("System reset is disabled") |
| 225 |
if test_core_reset: |
| 226 |
print("Software reset (default=VECTRESET)") |
| 227 |
target.selected_core.default_reset_type = Target.ResetType.VECTRESET |
| 228 |
fnc(reset_type=None) |
| 229 |
else: |
| 230 |
print("Core reset is disabled") |
| 231 |
if test_emulated_reset: |
| 232 |
print("Software reset (default=EMULATED)") |
| 233 |
target.selected_core.default_reset_type = Target.ResetType.EMULATED |
| 234 |
fnc(reset_type=None) |
| 235 |
else: |
| 236 |
print("Emulated reset is disabled") |
| 237 |
|
| 238 |
if test_system_reset: |
| 239 |
print("(Default) Software reset (SYSRESETREQ)") |
| 240 |
target.selected_core.default_reset_type = Target.ResetType.SYSRESETREQ |
| 241 |
fnc(reset_type=Target.ResetType.DEFAULT) |
| 242 |
if test_core_reset: |
| 243 |
print("(Default) Software reset (VECTRESET)") |
| 244 |
target.selected_core.default_reset_type = Target.ResetType.VECTRESET |
| 245 |
fnc(reset_type=Target.ResetType.DEFAULT) |
| 246 |
if test_emulated_reset: |
| 247 |
print("(Default) Software reset (EMULATED)") |
| 248 |
target.selected_core.default_reset_type = Target.ResetType.EMULATED |
| 249 |
fnc(reset_type=Target.ResetType.DEFAULT) |
| 250 |
|
| 251 |
if test_system_reset: |
| 252 |
print("Software reset (option=DEFAULT)") |
| 253 |
target.selected_core.default_reset_type = Target.ResetType.DEFAULT |
| 254 |
session.options['reset_type'] = 'default' |
| 255 |
fnc(reset_type=None) |
| 256 |
if test_hw_reset: |
| 257 |
print("Reset (option=NSRST)") |
| 258 |
session.options['reset_type'] = 'n_srst' |
| 259 |
fnc(reset_type=None) |
| 260 |
if test_system_reset: |
| 261 |
print("Software reset (option=SYSRESETREQ)") |
| 262 |
session.options['reset_type'] = 'sysresetreq' |
| 263 |
fnc(reset_type=None) |
| 264 |
if test_core_reset: |
| 265 |
print("Software reset (option=VECTRESET)") |
| 266 |
session.options['reset_type'] = 'vectreset' |
| 267 |
fnc(reset_type=None) |
| 268 |
if test_emulated_reset: |
| 269 |
print("Software reset (option=EMULATED)") |
| 270 |
session.options['reset_type'] = 'emulated' |
| 271 |
fnc(reset_type=None) |
| 272 |
|
| 273 |
reset_methods(target.reset) |
| 274 |
|
| 275 |
|
| 276 |
test_pass_count += 1 |
| 277 |
test_count += 1 |
| 278 |
print("TEST PASSED") |
| 279 |
|
| 280 |
|
| 281 |
print("\n\n------ Testing Reset Halt ------") |
| 282 |
reset_methods(target.reset_and_halt) |
| 283 |
|
| 284 |
|
| 285 |
test_pass_count += 1 |
| 286 |
test_count += 1 |
| 287 |
print("TEST PASSED") |
| 288 |
|
| 289 |
|
| 290 |
print("\n\n------ Testing Register Read/Write ------") |
| 291 |
print("Reading r0") |
| 292 |
val = target.read_core_register('r0') |
| 293 |
origR0 = val |
| 294 |
rawVal = target.read_core_register_raw('r0') |
| 295 |
test_count += 1 |
| 296 |
if val == rawVal: |
| 297 |
test_pass_count += 1 |
| 298 |
print("TEST PASSED") |
| 299 |
else: |
| 300 |
print("TEST FAILED") |
| 301 |
|
| 302 |
print("Writing r0") |
| 303 |
target.write_core_register('r0', 0x12345678) |
| 304 |
val = target.read_core_register('r0') |
| 305 |
rawVal = target.read_core_register_raw('r0') |
| 306 |
test_count += 1 |
| 307 |
if val == 0x12345678 and rawVal == 0x12345678: |
| 308 |
test_pass_count += 1 |
| 309 |
print("TEST PASSED") |
| 310 |
else: |
| 311 |
print("TEST FAILED") |
| 312 |
|
| 313 |
print("Raw writing r0") |
| 314 |
target.write_core_register_raw('r0', 0x87654321) |
| 315 |
val = target.read_core_register('r0') |
| 316 |
rawVal = target.read_core_register_raw('r0') |
| 317 |
test_count += 1 |
| 318 |
if val == 0x87654321 and rawVal == 0x87654321: |
| 319 |
test_pass_count += 1 |
| 320 |
print("TEST PASSED") |
| 321 |
else: |
| 322 |
print("TEST FAILED") |
| 323 |
|
| 324 |
print("Read/write r0, r1, r2, r3") |
| 325 |
origRegs = target.read_core_registers_raw(['r0', 'r1', 'r2', 'r3']) |
| 326 |
target.write_core_registers_raw(['r0', 'r1', 'r2', 'r3'], [1, 2, 3, 4]) |
| 327 |
vals = target.read_core_registers_raw(['r0', 'r1', 'r2', 'r3']) |
| 328 |
passed = vals[0] == 1 and vals[1] == 2 and vals[2] == 3 and vals[3] == 4 |
| 329 |
test_count += 1 |
| 330 |
if passed: |
| 331 |
test_pass_count += 1 |
| 332 |
print("TEST PASSED") |
| 333 |
else: |
| 334 |
print("TEST FAILED") |
| 335 |
|
| 336 |
|
| 337 |
origRegs[0] = origR0 |
| 338 |
target.write_core_registers_raw(['r0', 'r1', 'r2', 'r3'], origRegs) |
| 339 |
|
| 340 |
print("Verify exception is raised while core is running") |
| 341 |
target.resume() |
| 342 |
try: |
| 343 |
val = target.read_core_register('r0') |
| 344 |
except exceptions.CoreRegisterAccessError: |
| 345 |
passed = True |
| 346 |
else: |
| 347 |
passed = False |
| 348 |
test_count += 1 |
| 349 |
if passed: |
| 350 |
test_pass_count += 1 |
| 351 |
print("TEST PASSED") |
| 352 |
else: |
| 353 |
print("TEST FAILED") |
| 354 |
|
| 355 |
print("Verify failure to write core register while running raises exception") |
| 356 |
try: |
| 357 |
target.write_core_register('r0', 0x1234) |
| 358 |
except exceptions.CoreRegisterAccessError: |
| 359 |
passed = True |
| 360 |
else: |
| 361 |
passed = False |
| 362 |
test_count += 1 |
| 363 |
if passed: |
| 364 |
test_pass_count += 1 |
| 365 |
print("TEST PASSED") |
| 366 |
else: |
| 367 |
print("TEST FAILED") |
| 368 |
|
| 369 |
|
| 370 |
target.halt() |
| 371 |
|
| 372 |
if target.selected_core.has_fpu: |
| 373 |
print("Reading s0") |
| 374 |
val = target.read_core_register('s0') |
| 375 |
rawVal = target.read_core_register_raw('s0') |
| 376 |
origRawS0 = rawVal |
| 377 |
passed = isinstance(val, float) and isinstance(rawVal, int) \ |
| 378 |
and float32_to_u32(val) == rawVal |
| 379 |
test_count += 1 |
| 380 |
if passed: |
| 381 |
test_pass_count += 1 |
| 382 |
print("TEST PASSED") |
| 383 |
else: |
| 384 |
print("TEST FAILED") |
| 385 |
|
| 386 |
print("Writing s0") |
| 387 |
target.write_core_register('s0', math.pi) |
| 388 |
val = target.read_core_register('s0') |
| 389 |
rawVal = target.read_core_register_raw('s0') |
| 390 |
passed = float_compare(val, math.pi) and float_compare(u32_to_float32(rawVal), math.pi) |
| 391 |
test_count += 1 |
| 392 |
if passed: |
| 393 |
test_pass_count += 1 |
| 394 |
print("TEST PASSED") |
| 395 |
else: |
| 396 |
print("TEST FAILED (%f==%f, 0x%08x->%f)" % (val, math.pi, rawVal, u32_to_float32(rawVal))) |
| 397 |
|
| 398 |
print("Raw writing s0") |
| 399 |
x = float32_to_u32(32.768) |
| 400 |
target.write_core_register_raw('s0', x) |
| 401 |
val = target.read_core_register('s0') |
| 402 |
passed = float_compare(val, 32.768) |
| 403 |
test_count += 1 |
| 404 |
if passed: |
| 405 |
test_pass_count += 1 |
| 406 |
print("TEST PASSED") |
| 407 |
else: |
| 408 |
print("TEST FAILED (%f==%f)" % (val, 32.768)) |
| 409 |
|
| 410 |
print("Read/write s0, s1") |
| 411 |
_1p1 = float32_to_u32(1.1) |
| 412 |
_2p2 = float32_to_u32(2.2) |
| 413 |
origRegs = target.read_core_registers_raw(['s0', 's1']) |
| 414 |
target.write_core_registers_raw(['s0', 's1'], [_1p1, _2p2]) |
| 415 |
vals = target.read_core_registers_raw(['s0', 's1']) |
| 416 |
s0 = target.read_core_register('s0') |
| 417 |
s1 = target.read_core_register('s1') |
| 418 |
passed = vals[0] == _1p1 and float_compare(s0, 1.1) \ |
| 419 |
and vals[1] == _2p2 and float_compare(s1, 2.2) |
| 420 |
test_count += 1 |
| 421 |
if passed: |
| 422 |
test_pass_count += 1 |
| 423 |
print("TEST PASSED") |
| 424 |
else: |
| 425 |
print("TEST FAILED (0x%08x==0x%08x, %f==%f, 0x%08x==0x%08x, %f==%f)" \ |
| 426 |
% (vals[0], _1p1, s0, 1.1, vals[1], _2p2, s1, 2.2)) |
| 427 |
|
| 428 |
|
| 429 |
origRegs[0] = origRawS0 |
| 430 |
target.write_core_registers_raw(['s0', 's1'], origRegs) |
| 431 |
|
| 432 |
print("Verify that all listed core registers can be accessed") |
| 433 |
|
| 434 |
def test_reg_rw(r, new_value: int, test_write: bool) -> bool: |
| 435 |
did_pass = True |
| 436 |
try: |
| 437 |
|
| 438 |
original_val = target.read_core_register_raw(r.name) |
| 439 |
|
| 440 |
if not test_write: |
| 441 |
return did_pass |
| 442 |
|
| 443 |
|
| 444 |
if new_value == original_val: |
| 445 |
new_value = 0 |
| 446 |
|
| 447 |
|
| 448 |
target.write_core_register_raw(r.name, new_value) |
| 449 |
read_val = target.read_core_register_raw(r.name) |
| 450 |
if read_val != new_value: |
| 451 |
print(f"Failed to change value of register {r.name} to {new_value:#x}; read {read_val:#x}") |
| 452 |
did_pass = False |
| 453 |
|
| 454 |
target.write_core_register_raw(r.name, original_val) |
| 455 |
read_val = target.read_core_register_raw(r.name) |
| 456 |
if read_val != original_val: |
| 457 |
print(f"Failed to restore value of register {r.name} back to original {original_val:#x}; read {read_val:#x}") |
| 458 |
did_pass = False |
| 459 |
except exceptions.CoreRegisterAccessError: |
| 460 |
did_pass = False |
| 461 |
return did_pass |
| 462 |
|
| 463 |
reg_count = 0 |
| 464 |
passed_reg_count = 0 |
| 465 |
for r in target.selected_core.core_registers.as_set: |
| 466 |
test_write = True |
| 467 |
|
| 468 |
|
| 469 |
r_mask = (1 << r.bitsize) - 1 |
| 470 |
if 'sp' in r.name: |
| 471 |
r_mask &= ~0x3 |
| 472 |
elif r.name == 'pc': |
| 473 |
r_mask &= ~0x1 |
| 474 |
elif 'xpsr' in r.name: |
| 475 |
r_mask = 0xd0000000 |
| 476 |
elif 'control' == r.name: |
| 477 |
|
| 478 |
r_mask = 0x2 |
| 479 |
elif r.name in ('primask', 'faultmask'): |
| 480 |
r_mask = 0x1 |
| 481 |
elif r.name == 'basepri': |
| 482 |
r_mask = 0x80 |
| 483 |
elif r.name == 'fpscr': |
| 484 |
|
| 485 |
r_mask = 0xf7c0009f |
| 486 |
new_value = 0xdeadbeef & r_mask |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
if ((r.name in ('cfbp',)) |
| 493 |
or (('psr' in r.name) and (r.name != 'xpsr')) |
| 494 |
or ('_ns' in r.name) or ('_s' in r.name) |
| 495 |
): |
| 496 |
test_write = False |
| 497 |
|
| 498 |
reg_count += 1 |
| 499 |
if test_reg_rw(r, new_value, test_write): |
| 500 |
passed_reg_count += 1 |
| 501 |
|
| 502 |
test_count += 1 |
| 503 |
if passed_reg_count == reg_count: |
| 504 |
test_pass_count += 1 |
| 505 |
print("TEST PASSED (%i registers)" % reg_count) |
| 506 |
else: |
| 507 |
print("TEST FAILED (%i registers, %i failed)" % (reg_count, reg_count - passed_reg_count)) |
| 508 |
|
| 509 |
print("\n\n------ Testing Invalid Memory Access Recovery ------") |
| 510 |
memory_access_pass = True |
| 511 |
try: |
| 512 |
print("reading 0x1000 bytes at invalid address 0x%08x" % addr_invalid) |
| 513 |
target.read_memory_block8(addr_invalid, 0x1000) |
| 514 |
target.flush() |
| 515 |
|
| 516 |
if expect_invalid_access_to_fail: |
| 517 |
print(" failed to get expected fault") |
| 518 |
memory_access_pass = False |
| 519 |
else: |
| 520 |
print(" no fault as expected") |
| 521 |
except exceptions.TransferFaultError as exc: |
| 522 |
print(" got expected error: " + str(exc)) |
| 523 |
|
| 524 |
try: |
| 525 |
print("reading 0x1000 bytes at invalid address 0x%08x" % (addr_invalid + 1)) |
| 526 |
target.read_memory_block8(addr_invalid + 1, 0x1000) |
| 527 |
target.flush() |
| 528 |
|
| 529 |
if expect_invalid_access_to_fail: |
| 530 |
print(" failed to get expected fault") |
| 531 |
memory_access_pass = False |
| 532 |
else: |
| 533 |
print(" no fault as expected") |
| 534 |
except exceptions.TransferFaultError as exc: |
| 535 |
print(" got expected error: " + str(exc)) |
| 536 |
|
| 537 |
data = [0x00] * 0x1000 |
| 538 |
try: |
| 539 |
print("writing 0x%08x bytes at invalid address 0x%08x" % (len(data), addr_invalid)) |
| 540 |
target.write_memory_block8(addr_invalid, data) |
| 541 |
target.flush() |
| 542 |
|
| 543 |
if expect_invalid_access_to_fail: |
| 544 |
print(" failed to get expected fault!") |
| 545 |
memory_access_pass = False |
| 546 |
else: |
| 547 |
print(" no fault as expected") |
| 548 |
except exceptions.TransferFaultError as exc: |
| 549 |
print(" got expected error: " + str(exc)) |
| 550 |
|
| 551 |
data = [0x00] * 0x1000 |
| 552 |
try: |
| 553 |
print("writing 0x%08x bytes at invalid address 0x%08x" % (len(data), addr_invalid + 1)) |
| 554 |
target.write_memory_block8(addr_invalid + 1, data) |
| 555 |
target.flush() |
| 556 |
|
| 557 |
if expect_invalid_access_to_fail: |
| 558 |
print(" failed to get expected fault!") |
| 559 |
memory_access_pass = False |
| 560 |
else: |
| 561 |
print(" no fault as expected") |
| 562 |
except exceptions.TransferFaultError as exc: |
| 563 |
print(" got expected error: " + str(exc)) |
| 564 |
|
| 565 |
data = [randrange(0, 255) for x in range(size)] |
| 566 |
print("r/w 0x%08x bytes at 0x%08x" % (size, addr)) |
| 567 |
target.write_memory_block8(addr, data) |
| 568 |
block = target.read_memory_block8(addr, size) |
| 569 |
if same(data, block): |
| 570 |
print(" Aligned access pass") |
| 571 |
else: |
| 572 |
print(" Memory read does not match memory written") |
| 573 |
memory_access_pass = False |
| 574 |
|
| 575 |
data = [randrange(0, 255) for x in range(size)] |
| 576 |
print("r/w 0x%08x bytes at 0x%08x" % (size, addr + 1)) |
| 577 |
target.write_memory_block8(addr + 1, data) |
| 578 |
block = target.read_memory_block8(addr + 1, size) |
| 579 |
if same(data, block): |
| 580 |
print(" Unaligned access pass") |
| 581 |
else: |
| 582 |
print(" Unaligned memory read does not match memory written") |
| 583 |
memory_access_pass = False |
| 584 |
|
| 585 |
test_count += 1 |
| 586 |
if memory_access_pass: |
| 587 |
test_pass_count += 1 |
| 588 |
print("TEST PASSED") |
| 589 |
else: |
| 590 |
print("TEST FAILED") |
| 591 |
|
| 592 |
print("\n\n------ Testing Software Breakpoints ------") |
| 593 |
test_passed = True |
| 594 |
orig8x2 = target.read_memory_block8(addr, 2) |
| 595 |
orig8 = target.read8(addr) |
| 596 |
orig16 = target.read16(addr & ~1) |
| 597 |
orig32 = target.read32(addr & ~3) |
| 598 |
origAligned32 = target.read_memory_block32(addr & ~3, 1) |
| 599 |
|
| 600 |
def test_filters(): |
| 601 |
test_passed = True |
| 602 |
filtered = target.read_memory_block8(addr, 2) |
| 603 |
if same(orig8x2, filtered): |
| 604 |
print("2 byte unaligned passed") |
| 605 |
else: |
| 606 |
print("2 byte unaligned failed (read %x-%x, expected %x-%x)" % (filtered[0], filtered[1], orig8x2[0], orig8x2[1])) |
| 607 |
test_passed = False |
| 608 |
|
| 609 |
for now in (True, False): |
| 610 |
filtered = target.read8(addr, now) |
| 611 |
if not now: |
| 612 |
filtered = filtered() |
| 613 |
if filtered == orig8: |
| 614 |
print("8-bit passed [now=%s]" % now) |
| 615 |
else: |
| 616 |
print("8-bit failed [now=%s] (read %x, expected %x)" % (now, filtered, orig8)) |
| 617 |
test_passed = False |
| 618 |
|
| 619 |
filtered = target.read16(addr & ~1, now) |
| 620 |
if not now: |
| 621 |
filtered = filtered() |
| 622 |
if filtered == orig16: |
| 623 |
print("16-bit passed [now=%s]" % now) |
| 624 |
else: |
| 625 |
print("16-bit failed [now=%s] (read %x, expected %x)" % (now, filtered, orig16)) |
| 626 |
test_passed = False |
| 627 |
|
| 628 |
filtered = target.read32(addr & ~3, now) |
| 629 |
if not now: |
| 630 |
filtered = filtered() |
| 631 |
if filtered == orig32: |
| 632 |
print("32-bit passed [now=%s]" % now) |
| 633 |
else: |
| 634 |
print("32-bit failed [now=%s] (read %x, expected %x)" % (now, filtered, orig32)) |
| 635 |
test_passed = False |
| 636 |
|
| 637 |
filtered = target.read_memory_block32(addr & ~3, 1) |
| 638 |
if same(filtered, origAligned32): |
| 639 |
print("32-bit aligned passed") |
| 640 |
else: |
| 641 |
print("32-bit aligned failed (read %x, expected %x)" % (filtered[0], origAligned32[0])) |
| 642 |
test_passed = False |
| 643 |
return test_passed |
| 644 |
|
| 645 |
print("Installed software breakpoint at 0x%08x" % addr) |
| 646 |
target.set_breakpoint(addr, Target.BreakpointType.SW) |
| 647 |
test_passed = test_filters() and test_passed |
| 648 |
|
| 649 |
print("Removed software breakpoint") |
| 650 |
target.remove_breakpoint(addr) |
| 651 |
test_passed = test_filters() and test_passed |
| 652 |
|
| 653 |
test_count += 1 |
| 654 |
if test_passed: |
| 655 |
test_pass_count += 1 |
| 656 |
print("TEST PASSED") |
| 657 |
else: |
| 658 |
print("TEST FAILED") |
| 659 |
|
| 660 |
print("\nTest Summary:") |
| 661 |
print("Pass count %i of %i tests" % (test_pass_count, test_count)) |
| 662 |
if test_pass_count == test_count: |
| 663 |
print("CORTEX TEST PASSED") |
| 664 |
else: |
| 665 |
print("CORTEX TEST FAILED") |
| 666 |
|
| 667 |
target.reset() |
| 668 |
|
| 669 |
result.passed = test_count == test_pass_count |
| 670 |
return result |
| 671 |
|
| 672 |
if __name__ == "__main__": |
| 673 |
parser = argparse.ArgumentParser(description='pyOCD cpu test') |
| 674 |
parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging') |
| 675 |
args = parser.parse_args() |
| 676 |
level = logging.DEBUG if args.debug else logging.INFO |
| 677 |
logging.basicConfig(level=level) |
| 678 |
cortex_test(None) |