RAFlasher.py: logic to check if connection already established
[renesas-ra-flasher] / src / RAFlasher.py
index 75a4561..7c534ab 100644 (file)
@@ -12,6 +12,18 @@ def int_to_hex_list(num):
     hex_list = [f'0x{hex_string[c:c+2]}' for c in range(0, 8, 2)]
     return hex_list
 
+def inquire_connection(dev):
+    packed = pack_pkt(INQ_CMD, "")
+    dev.send_data(packed)
+    info = dev.recv_data(7)
+    print(info)
+    if info == bytearray(b'\x00') or info == bytearray(b''):
+        return False
+    msg = unpack_pkt(info)
+    print("Connection already established")
+    print(msg)
+    return True
+
 def get_dev_info(dev):
 
     packed = pack_pkt(SIG_CMD, "")
@@ -22,7 +34,19 @@ def get_dev_info(dev):
     #info = b'\x81\x00\x0D\x3A\x01\x31\x2d\x00\x00\x1e\x84\x80\x04\x02\x0a\x08' # test
     fmt = '>IIIBBHH'
     _HEADER, SCI, RMB, NOA, TYP, BFV, _FOOTER = struct.unpack(fmt, info)
-    print(f'Ver{BFV >> 8}.{BFV & 0xFF}')
+    print('Chip info:')
+    print('====================')
+    print(f'Serial interface speed: {SCI} Hz')
+    print(f'Recommend max UART baud rate {RMB} bps')
+    if TYP == 0x02:
+        print('RA MCU + RA2/RA4 Series')
+    elif TYP == 0x03:
+        print('RA MCU + RA6 Series')
+    else:
+        print('Unknown MCU type')
+    print(f'Boot firmware version {BFV >> 8}.{BFV & 0xFF}')
+    print('====================')
+
 
 def verify_img(dev, img, start_addr, end_addr):
     raise Exception("Not implemented")
@@ -103,8 +127,10 @@ def main():
         print('read command')
     elif args.command == "info":
         dev = RAConnect(vendor_id=0x045B, product_id=0x0261)
-        dev.establish_connection()
-        dev.confirm_connection()
+        status_con = inquire_connection(dev)
+        if not status_con:
+            #dev.establish_connection()
+            dev.confirm_connection()
         get_dev_info(dev)
     else:
         parser.print_help()