1#!/usr/bin/env python3
2#
3# Copyright (c) 2019 Intel Corporation.
4#
5# SPDX-License-Identifier: Apache-2.0
6"""
7Script to capture tracing data with UART backend.
8"""
9
10import sys
11import serial
12import argparse
13
14def parse_args():
15    global args
16    parser = argparse.ArgumentParser(
17        description=__doc__,
18        formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False)
19    parser.add_argument("-d", "--serial_port", required=True,
20                        help="serial port")
21    parser.add_argument("-b", "--serial_baudrate", required=True,
22                        help="serial baudrate")
23    parser.add_argument("-o", "--output", default='channel0_0',
24                        required=False, help="tracing data output file")
25    args = parser.parse_args()
26
27def main():
28    parse_args()
29    serial_port = args.serial_port
30    serial_baudrate = args.serial_baudrate
31    output_file = args.output
32    try:
33        ser = serial.Serial(serial_port, serial_baudrate)
34        ser.isOpen()
35    except serial.SerialException as e:
36        sys.exit("{}".format(e))
37
38    print("serial open success")
39
40    #enable device tracing
41    ser.write("enable\r".encode())
42
43    with open(output_file, "wb") as file_desc:
44        while True:
45            count = ser.inWaiting()
46            if count > 0:
47                while count > 0:
48                    data = ser.read()
49                    file_desc.write(data)
50                    count -= 1
51
52    ser.close()
53
54if __name__=="__main__":
55    try:
56        main()
57    except KeyboardInterrupt:
58        print('Data capture interrupted, data saved into {}'.format(args.output))
59        sys.exit(0)
60