|
| 1 | +import pytest |
| 2 | +import logging |
| 3 | +import argparse |
| 4 | +import textwrap |
| 5 | +import glob |
| 6 | +import os |
| 7 | +import json |
| 8 | +import serial |
| 9 | +import time |
| 10 | +import subprocess |
| 11 | +import re |
| 12 | + |
| 13 | +from pymcuprog.backend import SessionConfig |
| 14 | +from pymcuprog.toolconnection import ToolUsbHidConnection |
| 15 | +from pymcuprog.backend import Backend |
| 16 | +from pymcuprog.hexfileutils import read_memories_from_hex |
| 17 | +from pymcuprog.deviceinfo.memorynames import MemoryNameAliases |
| 18 | +from pymcuprog.deviceinfo.deviceinfokeys import DeviceMemoryInfoKeys |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +BOARD_CONFIG = "DxCore:megaavr:avrdb:appspm=no,chip=avr128db48,clock=24internal,"\ |
| 23 | + "bodvoltage=1v9,bodmode=disabled,eesave=enable,resetpin=reset,"\ |
| 24 | + "millis=tcb2,startuptime=8,wiremode=mors2,printf=full" |
| 25 | + |
| 26 | +TIMEOUT = 30 |
| 27 | + |
| 28 | +def program(request): |
| 29 | + """Builds and programs the sketch file |
| 30 | +
|
| 31 | + Args: |
| 32 | + request (obj): PyTest request object |
| 33 | + """ |
| 34 | + |
| 35 | + |
| 36 | + example_name = request.node.name[5:] |
| 37 | + |
| 38 | + build_directory = request.config.getoption("--builddir") |
| 39 | + sketch_directory = request.config.getoption("--sketchdir") |
| 40 | + |
| 41 | + # TODO: fix pathing |
| 42 | + sketch_path = f"{sketch_directory}/{example_name}/{example_name}.ino" |
| 43 | + |
| 44 | + print(f"Buillding and programming {os.path.basename(sketch_path)}...") |
| 45 | + |
| 46 | + if not os.path.exists(build_directory): |
| 47 | + os.mkdir(build_directory) |
| 48 | + |
| 49 | + compilation_return_code = subprocess.run([f"arduino-cli compile {sketch_path} -b {BOARD_CONFIG} --output-dir {build_directory}"], shell=True).returncode |
| 50 | + |
| 51 | + assert compilation_return_code == 0 |
| 52 | + |
| 53 | + |
| 54 | + hex_file = build_directory + f"/{os.path.basename(sketch_path)}.hex" |
| 55 | + |
| 56 | + try: |
| 57 | + memory_segments = read_memories_from_hex(hex_file, backend.device_memory_info) |
| 58 | + backend.erase(MemoryNameAliases.ALL, address=None) |
| 59 | + |
| 60 | + for segment in memory_segments: |
| 61 | + memory_name = segment.memory_info[DeviceMemoryInfoKeys.NAME] |
| 62 | + backend.write_memory(segment.data, memory_name, segment.offset) |
| 63 | + verify_ok = backend.verify_memory(segment.data, memory_name, segment.offset) |
| 64 | + |
| 65 | + if verify_ok: |
| 66 | + logging.info("OK") |
| 67 | + else: |
| 68 | + logging.error("Verification failed!") |
| 69 | + return False |
| 70 | + |
| 71 | + except Exception as exception: |
| 72 | + logging.warning(f"Error programming: {exception}") |
| 73 | + return False |
| 74 | + |
| 75 | + return True |
| 76 | + |
| 77 | +def test_debug_modem(request): |
| 78 | + # TODO: backend for pymcuprog has to be defined as a prerequisite and set up before all tests |
| 79 | + |
| 80 | + |
| 81 | + test = program(request) |
| 82 | + |
| 83 | + assert test == "debug_modem" |
0 commit comments