@@ -515,7 +515,8 @@ def device_is_available(self, instance):
515
515
for d in self .duts :
516
516
if fixture and fixture not in map (lambda f : f .split (sep = ':' )[0 ], d .fixtures ):
517
517
continue
518
- if d .platform != device or (d .serial is None and d .serial_pty is None ):
518
+ if d .platform != device or (d .serial is None and d .serial_pty is None and
519
+ d .use_rtt is False ):
519
520
continue
520
521
duts_found .append (d )
521
522
@@ -565,7 +566,7 @@ def run_custom_script(script, timeout):
565
566
proc .communicate ()
566
567
logger .error (f"{ script } timed out" )
567
568
568
- def _create_flash_command (self , hardware ):
569
+ def _create_flash_command_from_option (self , hardware ):
569
570
flash_command = next (csv .reader ([self .options .flash_command ]))
570
571
571
572
command = [flash_command [0 ]]
@@ -579,9 +580,28 @@ def _create_flash_command(self, hardware):
579
580
580
581
return command
581
582
582
- def _create_command (self , runner , hardware ):
583
+ def _create_rtt_command (self , hardware ):
584
+
585
+ command = ["west" , "rtt" , "--skip-rebuild" , "-d" , self .build_dir ]
586
+ rtt_runner = hardware .rtt_runner
587
+
588
+ if rtt_runner :
589
+ command .append ("--runner" )
590
+ command .append (rtt_runner )
591
+
592
+ if rtt_runner in ("jlink" , "pyocd" ):
593
+ board_id = hardware .probe_id or hardware .id
594
+
595
+ command .append ("--dev-id" )
596
+ command .append (board_id )
597
+
598
+ # Since _start_pty expects a string, we need to convert the command list into
599
+ # one.
600
+ return " " .join (command )
601
+
602
+ def _create_flash_command (self , runner , hardware ):
583
603
if self .options .flash_command :
584
- return self ._create_flash_command (hardware )
604
+ return self ._create_flash_command_from_option (hardware )
585
605
586
606
command = ["west" ]
587
607
if self .options .verbose > 2 :
@@ -648,18 +668,18 @@ def _create_command(self, runner, hardware):
648
668
649
669
return command
650
670
651
- def _terminate_pty (self , ser_pty , ser_pty_process ):
652
- logger .debug (f"Terminating serial- pty:'{ ser_pty } '" )
653
- terminate_process (ser_pty_process )
671
+ def _terminate_pty (self , pty_name , pty_process ):
672
+ logger .debug (f"Terminating pty:'{ pty_name } '" )
673
+ terminate_process (pty_process )
654
674
try :
655
- (stdout , stderr ) = ser_pty_process .communicate (timeout = self .get_test_timeout ())
656
- logger .debug (f"Terminated serial- pty:'{ ser_pty } ', stdout:'{ stdout } ', stderr:'{ stderr } '" )
675
+ (stdout , stderr ) = pty_process .communicate (timeout = self .get_test_timeout ())
676
+ logger .debug (f"Terminated pty:'{ pty_name } ', stdout:'{ stdout } ', stderr:'{ stderr } '" )
657
677
except subprocess .TimeoutExpired :
658
- logger .debug (f"Terminated serial- pty:'{ ser_pty } '" )
659
- #
678
+ logger .debug (f"Terminated pty:'{ pty_name } '" )
679
+
660
680
661
681
def _create_serial_connection (self , dut , serial_device , hardware_baud ,
662
- flash_timeout , serial_pty , ser_pty_process ):
682
+ flash_timeout , pty_name , pty_process ):
663
683
try :
664
684
ser = serial .Serial (
665
685
serial_device ,
@@ -671,20 +691,20 @@ def _create_serial_connection(self, dut, serial_device, hardware_baud,
671
691
timeout = max (flash_timeout , self .get_test_timeout ())
672
692
)
673
693
except serial .SerialException as e :
674
- self ._handle_serial_exception (e , dut , serial_pty , ser_pty_process )
694
+ self ._handle_serial_exception (e , dut , pty_name , pty_process )
675
695
raise
676
696
677
697
return ser
678
698
679
699
680
- def _handle_serial_exception (self , exception , dut , serial_pty , ser_pty_process ):
700
+ def _handle_serial_exception (self , exception , dut , pty_name , pty_process ):
681
701
self .instance .status = TwisterStatus .FAIL
682
702
self .instance .reason = "Serial Device Error"
683
703
logger .error (f"Serial device error: { exception !s} " )
684
704
685
705
self .instance .add_missing_case_status (TwisterStatus .BLOCK , "Serial Device Error" )
686
- if serial_pty and ser_pty_process :
687
- self ._terminate_pty (serial_pty , ser_pty_process )
706
+ if pty_name and pty_process :
707
+ self ._terminate_pty (pty_name , pty_process )
688
708
689
709
self .make_dut_available (dut )
690
710
@@ -706,21 +726,21 @@ def get_hardware(self):
706
726
logger .error (self .instance .reason )
707
727
return hardware
708
728
709
- def _start_serial_pty (self , serial_pty , serial_pty_master ):
710
- ser_pty_process = None
729
+ def _start_pty (self , command , pty_master ):
730
+ pty_process = None
711
731
try :
712
- ser_pty_process = subprocess .Popen (
713
- re .split ('[, ]' , serial_pty ),
714
- stdout = serial_pty_master ,
715
- stdin = serial_pty_master ,
716
- stderr = serial_pty_master
732
+ pty_process = subprocess .Popen (
733
+ re .split ('[, ]' , command ),
734
+ stdout = pty_master ,
735
+ stdin = pty_master ,
736
+ stderr = pty_master
717
737
)
718
738
except subprocess .CalledProcessError as error :
719
739
logger .error (
720
- f"Failed to run subprocess { serial_pty } , error { error .output } "
740
+ f"Failed to run subprocess { command } , error { error .output } "
721
741
)
722
742
723
- return ser_pty_process
743
+ return pty_process
724
744
725
745
def handle (self , harness ):
726
746
runner = None
@@ -732,16 +752,19 @@ def handle(self, harness):
732
752
733
753
runner = hardware .runner or self .options .west_runner
734
754
serial_pty = hardware .serial_pty
735
-
736
- if not serial_pty :
737
- serial_device = hardware .serial
755
+ use_rtt = hardware .use_rtt
756
+ pty_name = None
757
+
758
+ if serial_pty or use_rtt :
759
+ pty_master , slave = pty .openpty ()
760
+ pty_name = os .ttyname (slave )
761
+ reason = "RTT" if use_rtt else "serial PTY"
762
+ logger .debug (f"Using pty { pty_name } for { reason } " )
738
763
else :
739
- ser_pty_master , slave = pty .openpty ()
740
- serial_device = os .ttyname (slave )
741
-
742
- logger .debug (f"Using serial device { serial_device } @ { hardware .baud } baud" )
764
+ serial_device = hardware .serial
765
+ logger .debug (f"Using serial device { serial_device } @ { hardware .baud } baud" )
743
766
744
- command = self ._create_command (runner , hardware )
767
+ flash_command = self ._create_flash_command (runner , hardware )
745
768
746
769
pre_script = hardware .pre_script
747
770
post_flash_script = hardware .post_flash_script
@@ -759,20 +782,24 @@ def handle(self, harness):
759
782
flash_timeout += self .get_test_timeout ()
760
783
761
784
serial_port = None
762
- ser_pty_process = None
785
+ pty_process = None
763
786
if hardware .flash_before is False :
764
787
if serial_pty :
765
- ser_pty_process = self ._start_serial_pty (serial_pty , ser_pty_master )
766
- serial_port = serial_device
788
+ pty_process = self ._start_pty (serial_pty , pty_master )
789
+ serial_port = pty_name
790
+ else :
791
+ serial_port = serial_device
767
792
793
+ # Open connection to serial device or if using serial pty just create ser object
794
+ # and open it later
768
795
try :
769
796
ser = self ._create_serial_connection (
770
797
hardware ,
771
798
serial_port ,
772
799
hardware .baud ,
773
800
flash_timeout ,
774
801
serial_pty ,
775
- ser_pty_process
802
+ pty_process
776
803
)
777
804
except serial .SerialException :
778
805
return
@@ -785,11 +812,13 @@ def handle(self, harness):
785
812
t .start ()
786
813
787
814
d_log = f"{ self .instance .build_dir } /device.log"
788
- logger .debug (f'Flash command: { command } ' , )
815
+ logger .debug (f'Flash command: { flash_command } ' )
789
816
failure_type = Handler .FailureType .NONE
790
817
try :
791
818
stdout = stderr = None
792
- with subprocess .Popen (command , stderr = subprocess .PIPE , stdout = subprocess .PIPE ) as proc :
819
+ with subprocess .Popen (flash_command ,
820
+ stderr = subprocess .PIPE ,
821
+ stdout = subprocess .PIPE ) as proc :
793
822
try :
794
823
(stdout , stderr ) = proc .communicate (timeout = flash_timeout )
795
824
# ignore unencodable unicode chars
@@ -828,10 +857,18 @@ def handle(self, harness):
828
857
# Connect to device after flashing it
829
858
if hardware .flash_before :
830
859
try :
831
- if serial_pty :
832
- ser_pty_process = self ._start_serial_pty (serial_pty , ser_pty_master )
833
- logger .debug (f"Attach serial device { serial_device } @ { hardware .baud } baud" )
834
- ser .port = serial_device
860
+ if use_rtt :
861
+ rtt_command = self ._create_rtt_command (hardware )
862
+ pty_process = self ._start_pty (rtt_command , pty_master )
863
+ logger .debug (f"Start RTT connection on pty { pty_name } " )
864
+ ser .port = pty_name
865
+ elif serial_pty :
866
+ pty_process = self ._start_pty (serial_pty , pty_master )
867
+ logger .debug (f"Start serial connection on pty { pty_name } " )
868
+ ser .port = pty_name
869
+ else :
870
+ logger .debug (f"Attach serial device { serial_device } @ { hardware .baud } baud" )
871
+ ser .port = serial_device
835
872
836
873
# Apply ESP32-specific RTS/DTR reset logic
837
874
if runner == "esp32" :
@@ -854,7 +891,7 @@ def handle(self, harness):
854
891
ser .open ()
855
892
856
893
except serial .SerialException as e :
857
- self ._handle_serial_exception (e , hardware , serial_pty , ser_pty_process )
894
+ self ._handle_serial_exception (e , hardware , pty_name , pty_process )
858
895
return
859
896
860
897
if failure_type != Handler .FailureType .FLASH :
@@ -876,8 +913,8 @@ def handle(self, harness):
876
913
if ser .isOpen ():
877
914
ser .close ()
878
915
879
- if serial_pty :
880
- self ._terminate_pty (serial_pty , ser_pty_process )
916
+ if pty_name :
917
+ self ._terminate_pty (pty_name , pty_process )
881
918
882
919
self .execution_time = time .time () - start_time
883
920
0 commit comments