#!/usr/bin/python # -*- coding: utf-8 -*- """ @Title: Python Remote API example on how to use T32_ExecuteCommand() and T32_ExecuteFunction() with ctypes @Description: Python Remote API example on how to use T32_ExecuteCommand() and T32_ExecuteFunction() with ctypes TRACE32's configuration file "config.t32" has to contain these lines: RCL=NETASSIST PORT=20000 The port value may be changed but has to match with the port number used in this python script. Example output using t32marm: The current CPU is b'NONE' The current CPU is b'CortexA9MPCore' @Keywords: ctypes, Python, T32_ExecuteCommand, T32_ExecuteFunction @Copyright: (C) 1989-2015 Lauterbach GmbH, licensed for use with TRACE32(R) only $Id: t32api.py 116756 2020-01-27 07:42:44Z jvogl $ """ import platform import ctypes # auto-detect the correct library if (platform.system() == "Windows") or (platform.system()[0:6] == "CYGWIN"): if ctypes.sizeof(ctypes.c_voidp) == 4: # WINDOWS 32bit t32api = ctypes.CDLL("./t32api.dll") # alternative using windows DLL search order: # t32api = ctypes.cdll.t32api else: # WINDOWS 64bit t32api = ctypes.CDLL("./t32api64.dll") # alternative using windows DLL search order: # t32api = ctypes.cdll.t32api64 elif platform.system() == "Darwin": # Mac OS X t32api = ctypes.CDLL("./t32api.dylib") else: if ctypes.sizeof(ctypes.c_voidp) == 4: # Linux 32bit t32api = ctypes.CDLL("./t32api.so") else: # Linux 64bit t32api = ctypes.CDLL("./t32api64.so") def check_error(error): """Raises a ValueError if error is not zero.""" if error != 0: raise ValueError(f"{error}") # declare the argument- and return-types of the functions used in this file t32api.T32_Config.argtypes = [ctypes.POINTER(ctypes.c_char)] t32api.T32_Config.restype = ctypes.c_int t32api.T32_Init.argtypes = [] t32api.T32_Init.restype = ctypes.c_int t32api.T32_Attach.argtypes = [ctypes.c_int] t32api.T32_Attach.restype = ctypes.c_int t32api.T32_Ping.argtypes = [] t32api.T32_Ping.restype = ctypes.c_int t32api.T32_Exit.argtypes = [] t32api.T32_Exit.restype = ctypes.c_int t32api.T32_ExecuteCommand.argtypes = [ctypes.POINTER(ctypes.c_char), ctypes.POINTER(ctypes.c_char), ctypes.c_uint32] t32api.T32_ExecuteCommand.restype = ctypes.c_int t32api.T32_ExecuteFunction.argtypes = [ ctypes.POINTER(ctypes.c_char), ctypes.POINTER(ctypes.c_char), ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32), ] t32api.T32_ExecuteFunction.restype = ctypes.c_int # configuration check_error(t32api.T32_Config(b"NODE=", b"localhost")) check_error(t32api.T32_Config(b"PORT=", b"20000")) check_error(t32api.T32_Config(b"PACKLEN=", b"1024")) # init check_error(t32api.T32_Init()) check_error(t32api.T32_Attach(1)) # configuration cpufamily = "RISCV" cpu = "RV64" try: c_buffer = (ctypes.c_char * 4096)() c_result_type = ctypes.c_uint32() # ping check_error(t32api.T32_Ping()) error = t32api.T32_ExecuteCommand(b"TargetSystem.NewInstance ARM /ARCHitecture ARM", c_buffer, len(c_buffer)) if error != 0: raise ValueError(error, c_buffer.value.decode()) error = t32api.T32_ExecuteCommand(b'IC ARM PRINT "XXX"', c_buffer, len(c_buffer)) if error != 0: raise ValueError(error, c_buffer.value.decode()) finally: # exit check_error(t32api.T32_Exit())