Skip to content

Commit

Permalink
twister: add flash-before option
Browse files Browse the repository at this point in the history
Add option to flash board before attach serial.

Current implementation performs the following sequence:

1. Open serial port to listen to board log output
2. Flash device

In case of ESP32 where it uses the same serial port
for both operations, flashing needs to come first.

This PR adds a twister option named --flash-before
which enables the process above, allowing tests to be
performed properly.

Signed-off-by: Lucas Tamborrino <[email protected]>
  • Loading branch information
LucasTambor authored and sylvioalves committed Mar 22, 2024
1 parent f336026 commit 73212d7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
14 changes: 14 additions & 0 deletions scripts/pylib/twister/twisterlib/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ def add_parse_arguments(parser = None):
when flash operation also executes test case on the platform.
""")

parser.add_argument("--flash-before", action="store_true", default=False,
help="""Flash device before attaching to serial port.
This is useful for devices that share the same port for programming
and serial console, where flash must come first.
""")

test_or_build.add_argument(
"-b", "--build-only", action="store_true", default="--prep-artifacts-for-testing" in sys.argv,
help="Only build the code, do not attempt to run the code on targets.")
Expand Down Expand Up @@ -793,6 +799,14 @@ def parse_arguments(parser, args, options = None):
logger.error("--device-flash-with-test requires --device_testing")
sys.exit(1)

if options.flash_before and options.device_flash_with_test:
logger.error("--device-flash-with-test does not apply when --flash-before is used")
sys.exit(1)

if options.flash_before and options.device_serial_pty:
logger.error("--device-serial-pty cannot be used when --flash-before is set (for now)")
sys.exit(1)

if options.shuffle_tests and options.subset is None:
logger.error("--shuffle-tests requires --subset")
sys.exit(1)
Expand Down
19 changes: 18 additions & 1 deletion scripts/pylib/twister/twisterlib/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ def monitor_serial(self, ser, halt_event, harness):
# from the test.
harness.capture_coverage = True

# Wait for serial connection
while not ser.isOpen():
time.sleep(0.1)

# Clear serial leftover.
ser.reset_input_buffer()

Expand Down Expand Up @@ -647,9 +651,13 @@ def handle(self, harness):
if hardware.flash_with_test:
flash_timeout += self.get_test_timeout()

serial_port = None
if hardware.flash_before is False:
serial_port = serial_device

try:
ser = self._create_serial_connection(
serial_device,
serial_port,
hardware.baud,
flash_timeout,
serial_pty,
Expand Down Expand Up @@ -703,6 +711,15 @@ def handle(self, harness):
if post_flash_script:
self.run_custom_script(post_flash_script, 30)

# Connect to device after flashing it
if hardware.flash_before:
try:
logger.debug(f"Attach serial device {serial_device} @ {hardware.baud} baud")
ser.port = serial_device
ser.open()
except serial.SerialException:
return

if not flash_error:
# Always wait at most the test timeout here after flashing.
t.join(self.get_test_timeout())
Expand Down
18 changes: 13 additions & 5 deletions scripts/pylib/twister/twisterlib/hardwaremap.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def __init__(self,
post_flash_script=None,
runner=None,
flash_timeout=60,
flash_with_test=False):
flash_with_test=False,
flash_before=False):

self.serial = serial
self.baud = serial_baud or 115200
Expand All @@ -63,6 +64,7 @@ def __init__(self,
self.product = product
self.runner = runner
self.runner_params = runner_params
self.flash_before = flash_before
self.fixtures = []
self.post_flash_script = post_flash_script
self.post_script = post_script
Expand Down Expand Up @@ -177,7 +179,8 @@ def discover(self):
False,
baud=self.options.device_serial_baud,
flash_timeout=self.options.device_flash_timeout,
flash_with_test=self.options.device_flash_with_test
flash_with_test=self.options.device_flash_with_test,
flash_before=self.options.flash_before,
)

elif self.options.device_serial_pty:
Expand All @@ -186,7 +189,8 @@ def discover(self):
self.options.pre_script,
True,
flash_timeout=self.options.device_flash_timeout,
flash_with_test=self.options.device_flash_with_test
flash_with_test=self.options.device_flash_with_test,
flash_before=False,
)

# the fixtures given by twister command explicitly should be assigned to each DUT
Expand All @@ -207,9 +211,9 @@ def summary(self, selected_platforms):
print(tabulate(table, headers=header, tablefmt="github"))


def add_device(self, serial, platform, pre_script, is_pty, baud=None, flash_timeout=60, flash_with_test=False):
def add_device(self, serial, platform, pre_script, is_pty, baud=None, flash_timeout=60, flash_with_test=False, flash_before=False):
device = DUT(platform=platform, connected=True, pre_script=pre_script, serial_baud=baud,
flash_timeout=flash_timeout, flash_with_test=flash_with_test
flash_timeout=flash_timeout, flash_with_test=flash_with_test, flash_before=flash_before
)
if is_pty:
device.serial_pty = serial
Expand All @@ -229,6 +233,9 @@ def load(self, map_file):
flash_with_test = dut.get('flash_with_test')
if flash_with_test is None:
flash_with_test = self.options.device_flash_with_test
flash_before = dut.get('flash_before')
if flash_before is None:
flash_before = self.options.flash_before and (not (flash_with_test or serial_pty))
platform = dut.get('platform')
id = dut.get('id')
runner = dut.get('runner')
Expand All @@ -251,6 +258,7 @@ def load(self, map_file):
serial_baud=baud,
connected=connected,
pre_script=pre_script,
flash_before=flash_before,
post_script=post_script,
post_flash_script=post_flash_script,
flash_timeout=flash_timeout,
Expand Down
3 changes: 3 additions & 0 deletions scripts/schemas/twister/hwmap-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ sequence:
"flash_with_test":
type: bool
required: false
"flash_before":
type: bool
required: false

0 comments on commit 73212d7

Please sign in to comment.