# Path: msp430/jtag/jtag.py
# ERASE_SEGMENT = 0       # Erase a segment.
# ERASE_MAIN    = 1       # Erase all MAIN memory.
# ERASE_ALL     = 2       # Erase all MAIN and INFORMATION memory.
# ERASE_TOTAL   = 3       # Erase all MAIN and INFORMATION memory including IP protected area
# VERIFICATION_MODE = 0   # Verify data downloaded to FLASH memories.
# RAMSIZE_OPTION    = 1   # Change RAM used to download and program flash blocks
# DEBUG_OPTION      = 2   # Set debug level. Enables debug outputs.
# FLASH_CALLBACK    = 3   # Set a callback for progress report during flash write void f(WORD count, WORD total)
# EMULATION_MODE  = 1     # 4xx emulation mode
# LOCKED_FLASH_ACCESS = 5 # Allows Locked Info Mem Segment A access (if set to '1')
# EDT_TRACE_MODE = 7
# INTERFACE_MODE = 8      # see INTERFACE_TYPE below
# SET_MDB_BEFORE_RUN = 9
# RAM_PRESERVE_MODE = 10  # Configure whether RAM content should be preserved/restored
# UNLOCK_BSL_MODE = 11    # F5x/F6x Flash BSL access
# DEVICE_CODE = 12        # just used internal for the device code of L092 and C092
# WRITE_EXTERNAL_MEMORY = 13 # set true to write the external SPI image of the L092
# DEBUG_LPM_X = 14        # set DEBUG_LPM_X true to start debugging of LPMx.5
# JTAG_SPEED = 15         # Configure JTAG speed
# TOTAL_ERASE_DEVICE = 16 # total device erase including IP protection
# JTAG_IF = 0             # 4 Wire JTAG protocol used 
# SPYBIWIRE_IF = 1        # 2 Wire (Spy-bi-wire) JTAG protocol used
# SPYBIWIREJTAG_IF = 2    # 2 Wire Devices accessed by 4wire JTAG protocol
# AUTOMATIC_IF = 3        # Protocol will be detected automatically
# FLASH_MARGINAL_READ_OFF = 0
# FLASH_MARGINAL_READ_0 = 1
# FLASH_MARGINAL_READ_1 = 2
# PUC_RESET = 1 << 0      # Power up clear (i.e., a "soft") reset.
# RST_RESET = 1 << 1      # RST/NMI (i.e., "hard") reset.
# VCC_RESET = 1 << 2      # Cycle Vcc (i.e., a "power on") reset.
# ALL_RESETS = PUC_RESET + RST_RESET + VCC_RESET
# FET_CONNECTION_LOST = 0     # System event FET connection is lost
# DEVICE_CONNECTION_LOST = 1  # System event device connection is lost
# FET_RESTART_NEEDED = 2      # System event FET restart needed
# DEVICE_IN_LPM5_MODE = 3     # System event device entered LPMx.5
# DEVICE_WAKEUP_LPM5_MODE = 4 # System event devices wakes up from LPMx.5
# SYSTEM_NOTIFY_CALLBACK = ctypes.CFUNCTYPE(None, ctypes.c_int) # param is enum SYSTEM_EVENT_MSP
# DEBUG = 0
#     DEBUG = 1
# CTYPES_MSPGCC = "ctypes/mspgcc"
# CTYPES_TI = "ctypes/TI or 3rd party"
#     STATUS_OK    = 0
#     STATUS_ERROR = -1
#     TRUE         = 1
#     FALSE        = 0
#     WRITE        = 0
#     READ         = 1
#         STATUS_T = ctypes.c_long
#             MSP430_SET_SYSTEM_NOTIFY_CALLBACK             = MSP430mspgcc.MSP430_SET_SYSTEM_NOTIFY_CALLBACK
#     MSP430_VCC                      = MSP430mspgcc.MSP430_VCC
#         DEBUG = level
# class JTAGException(Exception): pass
#     class MSP430Library(object):
# class JTAG(object):
#         class SegmentEraser:
# def locate_library(libname, paths=sys.path, loader=None, verbose=0):
# def init_backend(force=None, verbose=0):
#             def MSP430_Open():
#             def MSP430_Open():
#         def MSP430_Secure():
#         def open(self, port = None):
#         def connect(self,):
#         def release(self):
#         def reset(self, execute = 0, release = 0, resets = ALL_RESETS):
#         def memread(self, address, size):
#         def memwrite(self, address, buffer):
#         def memverify(self, address, buffer):
#         def memerase(self, type=ERASE_ALL, address=0xfffe, length=2):
#         def funclet(self, code, timeout=1000):
#         def configure(self, mode, value = 0):
#         def regread(self, regnum):
#         def regwrite(self, regnum, value):
#         def set_flash_callback(self, function):
#         def isHalted(self):
#         def secure(self):
#     def __init__(self):
#     def open(self, lpt=None):
#     def connect(self):
#     def close(self):
#     def setDebugLevel(self, level):
#     def setRamsize(self, ramsize):
#     def downloadData(self, startaddress, data):
#     def uploadData(self, startaddress, size):
#     def reset(self, execute=0, release=0):
#     def getCPURegister(self, regnum):
#     def setCPURegister(self, regnum, value):
#     def actionMassErase(self):
#     def actionMainErase(self):
#     def makeActionSegmentErase(self, address):
#             def __init__(self, segaddr, verbose=0):
#             def __call__(self):
#             def __repr__(self):
#     def actionEraseCheck(self):
#     def progess_update(self, count, total):
#     def actionProgram(self):
#     def actionVerify(self):
#     def actionRun(self, address):
#     def actionFunclet(self, timeout=1):
#     def actionSecure(self):


from msp430.jtag import jtag
    from optparse import OptionParser
import sys
import os
import time
the problem there is, that it is not sure that we're reading a valid address
every time. an other issue is the relatively slow sampling rate compared to
the execution speed of the MCU, which means that several runs are need to
get meaningful numbers.
"""


if __name__ == '__main__':

    parser = OptionParser(usage="%prog [OPTIONS]")

    parser.add_option("-v", "--verbose",
            dest="verbose",
            help="show more messages (can be given multiple times)",
            default=0,
            action='count')

    parser.add_option("-o", "--output",
            dest="output",
            help="write result to given file",
            metavar="FILENAME")

    (options, args) = parser.parse_args()

    # prepare output
    if options.output is None:
        output = sys.stdout
    else:
        output = open(options.output, 'w')


# Path: msp430/jtag/jtag.py
# ERASE_SEGMENT = 0       # Erase a segment.
# ERASE_MAIN    = 1       # Erase all MAIN memory.
# ERASE_ALL     = 2       # Erase all MAIN and INFORMATION memory.
# ERASE_TOTAL   = 3       # Erase all MAIN and INFORMATION memory including IP protected area
# VERIFICATION_MODE = 0   # Verify data downloaded to FLASH memories.
# RAMSIZE_OPTION    = 1   # Change RAM used to download and program flash blocks
# DEBUG_OPTION      = 2   # Set debug level. Enables debug outputs.
# FLASH_CALLBACK    = 3   # Set a callback for progress report during flash write void f(WORD count, WORD total)
# EMULATION_MODE  = 1     # 4xx emulation mode
# LOCKED_FLASH_ACCESS = 5 # Allows Locked Info Mem Segment A access (if set to '1')
# EDT_TRACE_MODE = 7
# INTERFACE_MODE = 8      # see INTERFACE_TYPE below
# SET_MDB_BEFORE_RUN = 9
# RAM_PRESERVE_MODE = 10  # Configure whether RAM content should be preserved/restored
# UNLOCK_BSL_MODE = 11    # F5x/F6x Flash BSL access
# DEVICE_CODE = 12        # just used internal for the device code of L092 and C092
# WRITE_EXTERNAL_MEMORY = 13 # set true to write the external SPI image of the L092
# DEBUG_LPM_X = 14        # set DEBUG_LPM_X true to start debugging of LPMx.5
# JTAG_SPEED = 15         # Configure JTAG speed
# TOTAL_ERASE_DEVICE = 16 # total device erase including IP protection
# JTAG_IF = 0             # 4 Wire JTAG protocol used 
# SPYBIWIRE_IF = 1        # 2 Wire (Spy-bi-wire) JTAG protocol used
# SPYBIWIREJTAG_IF = 2    # 2 Wire Devices accessed by 4wire JTAG protocol
# AUTOMATIC_IF = 3        # Protocol will be detected automatically
# FLASH_MARGINAL_READ_OFF = 0
# FLASH_MARGINAL_READ_0 = 1
# FLASH_MARGINAL_READ_1 = 2
# PUC_RESET = 1 << 0      # Power up clear (i.e., a "soft") reset.
# RST_RESET = 1 << 1      # RST/NMI (i.e., "hard") reset.
# VCC_RESET = 1 << 2      # Cycle Vcc (i.e., a "power on") reset.
# ALL_RESETS = PUC_RESET + RST_RESET + VCC_RESET
# FET_CONNECTION_LOST = 0     # System event FET connection is lost
# DEVICE_CONNECTION_LOST = 1  # System event device connection is lost
# FET_RESTART_NEEDED = 2      # System event FET restart needed
# DEVICE_IN_LPM5_MODE = 3     # System event device entered LPMx.5
# DEVICE_WAKEUP_LPM5_MODE = 4 # System event devices wakes up from LPMx.5
# SYSTEM_NOTIFY_CALLBACK = ctypes.CFUNCTYPE(None, ctypes.c_int) # param is enum SYSTEM_EVENT_MSP
# DEBUG = 0
#     DEBUG = 1
# CTYPES_MSPGCC = "ctypes/mspgcc"
# CTYPES_TI = "ctypes/TI or 3rd party"
#     STATUS_OK    = 0
#     STATUS_ERROR = -1
#     TRUE         = 1
#     FALSE        = 0
#     WRITE        = 0
#     READ         = 1
#         STATUS_T = ctypes.c_long
#             MSP430_SET_SYSTEM_NOTIFY_CALLBACK             = MSP430mspgcc.MSP430_SET_SYSTEM_NOTIFY_CALLBACK
#     MSP430_VCC                      = MSP430mspgcc.MSP430_VCC
#         DEBUG = level
# class JTAGException(Exception): pass
#     class MSP430Library(object):
# class JTAG(object):
#         class SegmentEraser:
# def locate_library(libname, paths=sys.path, loader=None, verbose=0):
# def init_backend(force=None, verbose=0):
#             def MSP430_Open():
#             def MSP430_Open():
#         def MSP430_Secure():
#         def open(self, port = None):
#         def connect(self,):
#         def release(self):
#         def reset(self, execute = 0, release = 0, resets = ALL_RESETS):
#         def memread(self, address, size):
#         def memwrite(self, address, buffer):
#         def memverify(self, address, buffer):
#         def memerase(self, type=ERASE_ALL, address=0xfffe, length=2):
#         def funclet(self, code, timeout=1000):
#         def configure(self, mode, value = 0):
#         def regread(self, regnum):
#         def regwrite(self, regnum, value):
#         def set_flash_callback(self, function):
#         def isHalted(self):
#         def secure(self):
#     def __init__(self):
#     def open(self, lpt=None):
#     def connect(self):
#     def close(self):
#     def setDebugLevel(self, level):
#     def setRamsize(self, ramsize):
#     def downloadData(self, startaddress, data):
#     def uploadData(self, startaddress, size):
#     def reset(self, execute=0, release=0):
#     def getCPURegister(self, regnum):
#     def setCPURegister(self, regnum, value):
#     def actionMassErase(self):
#     def actionMainErase(self):
#     def makeActionSegmentErase(self, address):
#             def __init__(self, segaddr, verbose=0):
#             def __call__(self):
#             def __repr__(self):
#     def actionEraseCheck(self):
#     def progess_update(self, count, total):
#     def actionProgram(self):
#     def actionVerify(self):
#     def actionRun(self, address):
#     def actionFunclet(self, timeout=1):
#     def actionSecure(self):


# Path: msp430/jtag/profile.py
from msp430.jtag import jtag
    from optparse import OptionParser
import sys
import os
import time

the problem there is, that it is not sure that we're reading a valid address
every time. an other issue is the relatively slow sampling rate compared to
the execution speed of the MCU, which means that several runs are need to
get meaningful numbers.
"""


if __name__ == '__main__':

    parser = OptionParser(usage="%prog [OPTIONS]")

    parser.add_option("-v", "--verbose",
            dest="verbose",
            help="show more messages (can be given multiple times)",
            default=0,
            action='count')

    parser.add_option("-o", "--output",
            dest="output",
            help="write result to given file",
            metavar="FILENAME")

    (options, args) = parser.parse_args()

    # prepare output
    if options.output is None:
        output = sys.stdout
    else:
        output = open(options.output, 'w')


    jtag.init_backend(jtag.CTYPES_MSPGCC)   # doesn't currently work with 3'rd party libs