Skip to content

Commit

Permalink
Merge pull request #107 from espressif/feat/baud_fixture_move_to_seri…
Browse files Browse the repository at this point in the history
…al_service

Feat/baud fixture move to serial service
  • Loading branch information
hfudev authored Jul 25, 2022
2 parents b0c7302 + 7175622 commit 55a50fc
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 35 deletions.
25 changes: 22 additions & 3 deletions docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions pytest-embedded-arduino/pytest_embedded_arduino/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions pytest-embedded-idf/pytest_embedded_idf/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 6 additions & 8 deletions pytest-embedded-serial-esp/pytest_embedded_serial_esp/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class EspSerial(Serial):
stub: esptool.ESPStubLoader, stubbed loader.
"""

DEFAULT_BAUDRATE = 115200
ESPTOOL_DEFAULT_BAUDRATE = 921600

try:
# esptool>=4.0
Expand All @@ -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,
Expand Down Expand Up @@ -68,28 +69,25 @@ 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"')

# 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)

self.target = target

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)
Expand Down
10 changes: 8 additions & 2 deletions pytest-embedded-serial/pytest_embedded_serial/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()

Expand Down
33 changes: 25 additions & 8 deletions pytest-embedded/pytest_embedded/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand All @@ -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(
Expand Down Expand Up @@ -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 #
#######
Expand All @@ -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)


#######
Expand Down Expand Up @@ -722,6 +736,7 @@ def _fixture_classes_and_options(
baud,
skip_autoflash,
erase_all,
esptool_baud,
part_tool,
confirm_target_elf_sha256,
erase_nvs,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 55a50fc

Please sign in to comment.