From fe88a365d4cc82e9e1043d6259afb350ddc5eb5f Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Mon, 18 Jul 2022 18:27:54 +0800 Subject: [PATCH 1/2] feat(esp): add fixture `esptool_baud` feat(serial): move fixture `baud` to service `serial` --- .../pytest_embedded_arduino/serial.py | 13 ++++---- .../pytest_embedded_idf/serial.py | 13 ++++---- .../pytest_embedded_serial_esp/serial.py | 14 ++++---- .../pytest_embedded_serial/serial.py | 10 ++++-- pytest-embedded/pytest_embedded/plugin.py | 33 ++++++++++++++----- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/pytest-embedded-arduino/pytest_embedded_arduino/serial.py b/pytest-embedded-arduino/pytest_embedded_arduino/serial.py index d2de9d05..a31446bf 100644 --- a/pytest-embedded-arduino/pytest_embedded_arduino/serial.py +++ b/pytest-embedded-arduino/pytest_embedded_arduino/serial.py @@ -23,13 +23,16 @@ def __init__( app: ArduinoApp, port: Optional[str] = None, baud: int = EspSerial.DEFAULT_BAUDRATE, + esptool_baud: int = EspSerial.ESPTOOL_DEFAULT_BAUDRATE, target: Optional[str] = None, skip_autoflash: bool = False, erase_all: bool = False, **kwargs, ) -> None: self.app = app - super().__init__(pexpect_proc, target or self.app.target, port, baud, skip_autoflash, erase_all, **kwargs) + super().__init__( + pexpect_proc, target or self.app.target, port, baud, esptool_baud, skip_autoflash, erase_all, **kwargs + ) def _start(self): if self.skip_autoflash: @@ -76,14 +79,10 @@ def __init__(self, attributes): flash_args = FlashArgs(default_kwargs) try: - if self.proc.baudrate < self.SUGGEST_FLASH_BAUDRATE: - self.stub.change_baud(self.SUGGEST_FLASH_BAUDRATE) - + self.stub.change_baud(self.esptool_baud) esptool.detect_flash_size(self.stub, flash_args) esptool.write_flash(self.stub, flash_args) - - if self.proc.baudrate > self.DEFAULT_BAUDRATE: - self.stub.change_baud(self.DEFAULT_BAUDRATE) + self.stub.change_baud(self.baud) except Exception: raise finally: diff --git a/pytest-embedded-idf/pytest_embedded_idf/serial.py b/pytest-embedded-idf/pytest_embedded_idf/serial.py index 9fe6ba57..bff6fe8b 100644 --- a/pytest-embedded-idf/pytest_embedded_idf/serial.py +++ b/pytest-embedded-idf/pytest_embedded_idf/serial.py @@ -28,6 +28,7 @@ def __init__( target: Optional[str] = None, port: Optional[str] = None, baud: int = EspSerial.DEFAULT_BAUDRATE, + esptool_baud: int = EspSerial.ESPTOOL_DEFAULT_BAUDRATE, skip_autoflash: bool = False, erase_all: bool = False, port_app_cache: Dict[str, str] = None, @@ -46,7 +47,9 @@ def __init__( if target and self.app.target and self.app.target != target: raise ValueError(f'Targets do not match. App target: {self.app.target}, Cmd target: {target}.') - super().__init__(pexpect_proc, target or app.target, port, baud, skip_autoflash, erase_all, **kwargs) + super().__init__( + pexpect_proc, target or app.target, port, baud, esptool_baud, skip_autoflash, erase_all, **kwargs + ) def _post_init(self): if self.esp.serial_port in self._port_app_cache: @@ -138,14 +141,10 @@ def __init__(self, attributes): args = FlashArgs(default_kwargs) try: - if self.proc.baudrate < self.SUGGEST_FLASH_BAUDRATE: - self.stub.change_baud(self.SUGGEST_FLASH_BAUDRATE) - + self.stub.change_baud(self.esptool_baud) esptool.detect_flash_size(self.stub, args) esptool.write_flash(self.stub, args) - - if self.proc.baudrate > self.DEFAULT_BAUDRATE: - self.stub.change_baud(self.DEFAULT_BAUDRATE) # set to the default one to get the serial output + self.stub.change_baud(self.baud) finally: if nvs_file: nvs_file.close() diff --git a/pytest-embedded-serial-esp/pytest_embedded_serial_esp/serial.py b/pytest-embedded-serial-esp/pytest_embedded_serial_esp/serial.py index 47a34dcf..2ea92a34 100644 --- a/pytest-embedded-serial-esp/pytest_embedded_serial_esp/serial.py +++ b/pytest-embedded-serial-esp/pytest_embedded_serial_esp/serial.py @@ -22,7 +22,7 @@ class EspSerial(Serial): stub: esptool.ESPStubLoader, stubbed loader. """ - DEFAULT_BAUDRATE = 115200 + ESPTOOL_DEFAULT_BAUDRATE = 921600 try: # esptool>=4.0 @@ -40,7 +40,8 @@ def __init__( pexpect_proc: PexpectProcess, target: Optional[str] = None, port: Optional[str] = None, - baud: int = DEFAULT_BAUDRATE, + baud: int = Serial.DEFAULT_BAUDRATE, + esptool_baud: int = ESPTOOL_DEFAULT_BAUDRATE, skip_autoflash: bool = False, erase_all: bool = False, port_target_cache: Dict[str, str] = None, @@ -68,10 +69,9 @@ def __init__( ports = [port] with DuplicateStdout(pexpect_proc): - initial_baud = min(self.DEFAULT_BAUDRATE, baud) # don't sync faster than the default baud rate # normal loader self.esp: esptool.ESPLoader = esptool.get_default_connected_device( - ports, port=port, connect_attempts=3, initial_baud=initial_baud, chip=target + ports, port=port, connect_attempts=3, initial_baud=baud, chip=target ) if not self.esp: raise ValueError('Couldn\'t auto detect chip. Please manually specify with "--port"') @@ -79,9 +79,6 @@ def __init__( # stub loader has more functionalities, need to run after calling `run_stub()` self.stub: esptool.ESPLoader = self.esp.run_stub() - if baud > initial_baud: - self.esp.change_baud(baud) # change back to the users settings - target = self.esp.CHIP_NAME.lower().replace('-', '') logging.info(f'Target: %s, Port: %s', target, self.esp.serial_port) @@ -89,7 +86,8 @@ def __init__( self.skip_autoflash = skip_autoflash self.erase_all = erase_all - super().__init__(pexpect_proc, port=self.esp._port, **kwargs) + self.esptool_baud = esptool_baud + super().__init__(pexpect_proc, port=self.esp._port, baud=baud, **kwargs) def _post_init(self): logging.debug('set port-target cache: %s - %s', self.port, self.target) diff --git a/pytest-embedded-serial/pytest_embedded_serial/serial.py b/pytest-embedded-serial/pytest_embedded_serial/serial.py index 92f9ce2e..bf32b7be 100644 --- a/pytest-embedded-serial/pytest_embedded_serial/serial.py +++ b/pytest-embedded-serial/pytest_embedded_serial/serial.py @@ -17,8 +17,10 @@ class Serial(DuplicateStdoutMixin): proc (serial.Serial): process created by `serial.serial_for_url()` """ + DEFAULT_BAUDRATE = 115200 + DEFAULT_PORT_CONFIG = { - 'baudrate': 115200, + 'baudrate': DEFAULT_BAUDRATE, 'bytesize': pyserial.EIGHTBITS, 'parity': pyserial.PARITY_NONE, 'stopbits': pyserial.STOPBITS_ONE, @@ -29,7 +31,9 @@ class Serial(DuplicateStdoutMixin): occupied_ports: Dict[str, None] = dict() - def __init__(self, pexpect_proc: PexpectProcess, port: Union[str, pyserial.Serial], **kwargs): + def __init__( + self, pexpect_proc: PexpectProcess, port: Union[str, pyserial.Serial], baud: int = DEFAULT_BAUDRATE, **kwargs + ): """ Args: pexpect_proc: `PexpectProcess` instance @@ -43,6 +47,7 @@ def __init__(self, pexpect_proc: PexpectProcess, port: Union[str, pyserial.Seria if isinstance(port, str): self.port = port self.port_config = copy.deepcopy(self.DEFAULT_PORT_CONFIG) + self.port_config['baudrate'] = baud self.port_config.update(**kwargs) self.proc = pyserial.serial_for_url(self.port, **self.port_config) else: # pyserial instance @@ -51,6 +56,7 @@ def __init__(self, pexpect_proc: PexpectProcess, port: Union[str, pyserial.Seria self.port = self.proc.port self.pexpect_proc = pexpect_proc + self.baud = baud self._post_init() diff --git a/pytest-embedded/pytest_embedded/plugin.py b/pytest-embedded/pytest_embedded/plugin.py index 2af2422c..3ec0cc0d 100644 --- a/pytest-embedded/pytest_embedded/plugin.py +++ b/pytest-embedded/pytest_embedded/plugin.py @@ -107,10 +107,13 @@ def pytest_addoption(parser): serial_group = parser.getgroup('embedded-serial') serial_group.addoption('--port', help='serial port. (Env: "ESPPORT" if service "esp" specified, Default: "None")') + serial_group.addoption( + '--baud', + help='serial port communication baud rate. (Default: 115200)', + ) esp_group = parser.getgroup('embedded-esp') esp_group.addoption('--target', help='serial target chip type. (Default: "auto")') - esp_group.addoption('--baud', help='serial port baud rate used when flashing. (Env: "ESPBAUD", Default: 115200)') esp_group.addoption( '--skip-autoflash', help='y/yes/true for True and n/no/false for False. Set to True to disable auto flash. (Default: False)', @@ -120,6 +123,10 @@ def pytest_addoption(parser): help='y/yes/true for True and n/no/false for False. Set to True to erase all flash before programming. ' '(Default: False)', ) + esp_group.addoption( + '--esptool-baud', + help='esptool flashing baud rate. (Env: "ESPBAUD" if service "esp" specified, Default: 921600)', + ) idf_group = parser.getgroup('embedded-idf') idf_group.addoption( @@ -552,6 +559,13 @@ def port(request: FixtureRequest) -> Optional[str]: return _request_param_or_config_option_or_default(request, 'port', None) +@pytest.fixture +@multi_dut_argument +def baud(request: FixtureRequest) -> Optional[str]: + """Enable parametrization for the same cli option""" + return _request_param_or_config_option_or_default(request, 'baud', None) + + ####### # esp # ####### @@ -564,23 +578,23 @@ def target(request: FixtureRequest) -> Optional[str]: @pytest.fixture @multi_dut_argument -def baud(request: FixtureRequest) -> Optional[str]: +def skip_autoflash(request: FixtureRequest) -> Optional[bool]: """Enable parametrization for the same cli option""" - return _request_param_or_config_option_or_default(request, 'baud', None) + return _request_param_or_config_option_or_default(request, 'skip_autoflash', None) @pytest.fixture @multi_dut_argument -def skip_autoflash(request: FixtureRequest) -> Optional[bool]: +def erase_all(request: FixtureRequest) -> Optional[bool]: """Enable parametrization for the same cli option""" - return _request_param_or_config_option_or_default(request, 'skip_autoflash', None) + return _request_param_or_config_option_or_default(request, 'erase_all', None) @pytest.fixture @multi_dut_argument -def erase_all(request: FixtureRequest) -> Optional[bool]: +def esptool_baud(request: FixtureRequest) -> Optional[str]: """Enable parametrization for the same cli option""" - return _request_param_or_config_option_or_default(request, 'erase_all', None) + return _request_param_or_config_option_or_default(request, 'esptool_baud', None) ####### @@ -722,6 +736,7 @@ def _fixture_classes_and_options( baud, skip_autoflash, erase_all, + esptool_baud, part_tool, confirm_target_elf_sha256, erase_nvs, @@ -808,7 +823,8 @@ def _fixture_classes_and_options( 'pexpect_proc': pexpect_proc, 'target': target, 'port': os.getenv('ESPPORT') or port, - 'baud': int(os.getenv('ESPBAUD') or baud or EspSerial.DEFAULT_BAUDRATE), + 'baud': int(baud or EspSerial.DEFAULT_BAUDRATE), + 'esptool_baud': int(os.getenv('ESPBAUD') or esptool_baud or EspSerial.ESPTOOL_DEFAULT_BAUDRATE), 'skip_autoflash': skip_autoflash, 'erase_all': erase_all, } @@ -843,6 +859,7 @@ def _fixture_classes_and_options( kwargs[fixture] = { 'pexpect_proc': pexpect_proc, 'port': port, + 'baud': int(baud or Serial.DEFAULT_BAUDRATE), } elif fixture in ['openocd', 'gdb']: if 'jtag' in _services: From 71756226a9ce6f92a5dca16c393080f2f2fdb7e3 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Mon, 18 Jul 2022 18:28:48 +0800 Subject: [PATCH 2/2] docs: update the latest readme --- docs/services.md | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/services.md b/docs/services.md index a158964e..bfe44695 100644 --- a/docs/services.md +++ b/docs/services.md @@ -67,8 +67,8 @@ embedded: "--app-path=test_path1|test_path2" when two DUTs are using different built binary files. "--part-tool=part_tool_path|" when only the first DUT needs this option, the second should keep as empty. "--embedded-services=idf --count=2" when both of these DUTs are using the same services. - The configuration would be duplicated when it has only one value but the "count" amount is greater than 1. It would raise an - exception when the configuration has multi values but the amount is different from the "count" amount. + The configuration would be duplicated when it has only one value but the "count" amount is greater than 1. It would raise an exception when the + configuration has multi values but the amount is different from the "count" amount. For example: "--embedded-services=idf|esp-idf --count=3" would raise an exception. --parallel-count=PARALLEL_COUNT @@ -88,19 +88,36 @@ embedded: --app-path=APP_PATH App path --build-dir=BUILD_DIR build directory under the app_path. (Default: "build") + --with-timestamp=WITH_TIMESTAMP + y/yes/true for True and n/no/false for False. Set to True to enable print with timestamp. (Default: True) + --reorder-by-app-path + Reorder the test sequence according to the [app_path] and [build_dir]. (Default: False) embedded-serial: --port=PORT serial port. (Env: "ESPPORT" if service "esp" specified, Default: "None") + --baud=BAUD serial port communication baud rate. (Default: 115200) embedded-esp: --target=TARGET serial target chip type. (Default: "auto") - --baud=BAUD serial port baud rate used when flashing. (Env: "ESPBAUD", Default: 115200) --skip-autoflash=SKIP_AUTOFLASH y/yes/true for True and n/no/false for False. Set to True to disable auto flash. (Default: False) + --erase-all=ERASE_ALL + y/yes/true for True and n/no/false for False. Set to True to erase all flash before programming. (Default: False) + --esptool-baud=ESPTOOL_BAUD + esptool flashing baud rate. (Env: "ESPBAUD" if service "esp" specified, Default: 921600) embedded-idf: --part-tool=PART_TOOL Partition tool path, used for parsing partition table. (Default: "$IDF_PATH/components/partition_table/gen_esp32part.py" + --confirm-target-elf-sha256=CONFIRM_TARGET_ELF_SHA256 + y/yes/true for True and n/no/false for False. Set to True to read the elf sha256 from target flash and compare to the local elf under + app.binary_path when session target-app cache decide to skip the autoflash. (Default: False) + --erase-nvs=ERASE_NVS + y/yes/true for True and n/no/false for False. Set to True to erase the non-volatile storage blocks when flash files to the target chip. Requires + valid partition tool. (Default: False) + --skip-check-coredump=SKIP_CHECK_COREDUMP + y/yes/true for True and n/no/false for False. Set to True to skip auto check core dump in UART/flash while teardown the failing test case. Requires + valid partition tool, project_description.json under the build dir. (Default: False) embedded-jtag: --gdb-prog-path=GDB_PROG_PATH @@ -121,6 +138,8 @@ embedded-qemu: QEMU cli default arguments. (Default: "-nographic -no-reboot -machine esp32") --qemu-extra-args=QEMU_EXTRA_ARGS QEMU cli extra arguments, will append to the argument list. (Default: None) + --skip-regenerate-image=SKIP_REGENERATE_IMAGE + y/yes/true for True and n/no/false for False. Set to True to disable auto regenerate image. (Default: False) ``` ## Services