From 0efa8d4fd86917c0a0e59de6170115e6e98e5c16 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Mon, 13 Apr 2020 19:43:32 -0700 Subject: [PATCH] Add WIP test script Depends on having: 1. An STM32 Nucleo with an STM32L412RB connected 2. A serial device connected to that board 3. A JLink connected to that board (no the onboard ST-Link!) For now, there are some hardcoded paths, too. --- test/src/tr_test/test.py | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 test/src/tr_test/test.py diff --git a/test/src/tr_test/test.py b/test/src/tr_test/test.py new file mode 100755 index 0000000..a22b8db --- /dev/null +++ b/test/src/tr_test/test.py @@ -0,0 +1,99 @@ +# Copyright (C) 2020 Max Regan + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import pytest +import subprocess +import serial +import serial.tools.list_ports +import logging +import pylink +import time + +from typing import List + +SAMPLE_TIME_MS = 2500 +SAMPLES_PER_SEC = 12000000 +DRIVER = "fx2lafw" + + +@pytest.fixture +def logger(): + logging.basicConfig() + return logging.getLogger(__name__) + +@pytest.fixture +def context_factory(): + + def create_context(mcu: str, addr: int, fw_path: str, leave_halted: bool=False): + ports = serial.tools.list_ports.comports() + if len(ports) == 0: + raise RuntimeError("No serial devices found") + if len(ports) > 1: + raise RuntimeError( + "Too many serial devices, not sure which to use. " + "This should be made configurable" + ) + + jlink = pylink.jlink.JLink() + jlink.open() + jlink.disable_dialog_boxes() + jlink.set_tif(pylink.enums.JLinkInterfaces.SWD) + jlink.connect(mcu) + #jlink.set_reset_strategy(pylink.enums.JLinkResetStrategyCortexM3.HALT_AFTER_BTL) + jlink.flash_file(fw_path, addr) + assert jlink.halted() + jlink.reset(halt=True) + + serial_dev = serial.Serial(port=ports[0].device, baudrate=115200) + + if not leave_halted: + jlink.reset(halt=False) + while True: + try: + serial_dev.read_until(b"HELLO WORLD!\r\n") + except serial.serialutil.SerialException: + continue + break + + return serial_dev, jlink + + return create_context + + +def test_serial(context_factory, logger): + serial_dev, jlink = context_factory( + "STM32L412RB", + 0x8000000, + "/home/max/work/TimelyReference/firmware/watch.bin" + ) + + lines = [serial_dev.readline().decode("ascii", errors="ignore") for _ in range(3)] + logger.info("Test lines: {}".format(lines)) + + assert lines[0] == "Counter: 0\r\n" + assert lines[1] == "HELLO WORLD!\r\n" + assert lines[2] == "Counter: 1\r\n" + + +def main(): + pytest.main() + + +if __name__ == "__main__": + main()