# Path: msp430/bsl5/bsl5.py
# BSL_RX_DATA_BLOCK       = 0x10    # Write to boot loader
# BSL_RX_DATA_BLOCK_FAST  = 0x1b    # Write to boot loader
# BSL_RX_PASSWORD         = 0x11    # Receive password to unlock commands
# BSL_ERASE_SEGMENT       = 0x12    # Erase one segment
# BSL_LOCK_INFO           = 0x13    # Toggle INFO_A lock bit
# BSL_MASS_ERASE          = 0x15    # Erase complete FLASH memory
# BSL_CRC_CHECK           = 0x16    # Run 16 bit CRC check over given area
# BSL_LOAD_PC             = 0x17    # Load PC and start execution
# BSL_TX_DATA_BLOCK       = 0x18    # Read from boot loader
# BSL_VERSION             = 0x19    # Get BSL version
# BSL_BUFFER_SIZE         = 0x1a    # Get BSL buffer size
# BSL5_ERROR_CODES = {
#         0x00: 'Operation successful',
#         0x01: 'Flash write check failed',
#         0x02: 'Flash fail bit set',
#         0x03: 'Voltage change during program',
#         0x04: 'BSL locked',
#         0x05: 'BSL password error',
#         0x06: 'Byte write forbidden',
#         0x07: 'Unknown command',
#         0x08: 'Packet length exceeds buffer size',
# }
# class BSL5Exception(Exception):
# class BSL5Timeout(BSL5Exception):
# class BSL5Error(BSL5Exception):
# class BSL5(object):
#     class DummyBSL(BSL5):
# def three_bytes(address):
#     def check_answer(self, data):
#     def BSL_RX_DATA_BLOCK(self, address, data):
#     def BSL_RX_DATA_BLOCK_FAST(self, address, data):
#     def BSL_TX_DATA_BLOCK(self, address, length):
#     def BSL_MASS_ERASE(self):
#     def BSL_ERASE_SEGMENT(self, address):
#     def BSL_LOAD_PC(self, address):
#     def BSL_RX_PASSWORD(self, password):
#     def BSL_VERSION(self):
#     def BSL_BUFFER_SIZE(self):
#     def BSL_LOCK_INFO(self):
#     def BSL_CRC_CHECK(self):
#     def detect_buffer_size(self):
#     def __init__(self):
#     def memory_read(self, address, length):
#     def memory_write(self, address, data):
#     def mass_erase(self):
#     def erase(self, address):
#     def execute(self, address):
#     def password(self, password):
#     def version(self):
#     def reset(self):
#         def bsl(self, cmd, message='', expect=None):


import sys
import os
import struct
import logging
import time
import pkgutil
import msp430.target
import msp430.memory
    import ctypes
    import Queue
    import glob
from msp430.bsl5 import bsl5
from optparse import OptionGroup
from cStringIO import StringIO
    from pywinusb import hid

        If the parameter "expect" is not None, "expect" bytes are expected in
        the answer, an exception is raised if the answer length does not match.
        If "expect" is None, the answer is just returned.

        Frame format:
        +------+-----+-----------+
        | 0x3f | len | D1 ... DN |
        +------+-----+-----------+
        """
        # first synchronize with slave
        self.logger.debug('Command 0x%02x (%d bytes)' % (cmd, 1+len(message)))
        #~ self.logger.debug('Command 0x%02x %s (%d bytes)' % (cmd, message.encode('hex'), 1+len(message)))
        txdata = bytearray(struct.pack('<BBB', 0x3f, 1+len(message), cmd) + message)
        txdata += b'\xac'*(64 - len(txdata)) # pad up to block size
        #~ self.logger.debug('Sending command: %r %d Bytes' % (txdata.encode('hex'), len(txdata)))
        # transmit command
        self.write_report(txdata)
        if receive_response:
            self.logger.debug('Reading answer...')
            report = self.read_report()
            self.logger.debug('report = %r' % report.encode('hex'))
            pi = report[0]
            if pi == '\x3f':
                length = ord(report[1])
                data = report[2:2+length]
                #~ if expect is not None and len(data) != expect:
                    #~ raise bsl5.BSL5Error('expected %d bytes, got %d bytes' % (expect, len(data)))
                return data
            else:
                if pi: raise bsl5.BSL5Error('received bad PI, expected 0x3f (got 0x%02x)' % (ord(pi),))

# Path: msp430/bsl5/bsl5.py
# BSL_RX_DATA_BLOCK       = 0x10    # Write to boot loader
# BSL_RX_DATA_BLOCK_FAST  = 0x1b    # Write to boot loader
# BSL_RX_PASSWORD         = 0x11    # Receive password to unlock commands
# BSL_ERASE_SEGMENT       = 0x12    # Erase one segment
# BSL_LOCK_INFO           = 0x13    # Toggle INFO_A lock bit
# BSL_MASS_ERASE          = 0x15    # Erase complete FLASH memory
# BSL_CRC_CHECK           = 0x16    # Run 16 bit CRC check over given area
# BSL_LOAD_PC             = 0x17    # Load PC and start execution
# BSL_TX_DATA_BLOCK       = 0x18    # Read from boot loader
# BSL_VERSION             = 0x19    # Get BSL version
# BSL_BUFFER_SIZE         = 0x1a    # Get BSL buffer size
# BSL5_ERROR_CODES = {
#         0x00: 'Operation successful',
#         0x01: 'Flash write check failed',
#         0x02: 'Flash fail bit set',
#         0x03: 'Voltage change during program',
#         0x04: 'BSL locked',
#         0x05: 'BSL password error',
#         0x06: 'Byte write forbidden',
#         0x07: 'Unknown command',
#         0x08: 'Packet length exceeds buffer size',
# }
# class BSL5Exception(Exception):
# class BSL5Timeout(BSL5Exception):
# class BSL5Error(BSL5Exception):
# class BSL5(object):
#     class DummyBSL(BSL5):
# def three_bytes(address):
#     def check_answer(self, data):
#     def BSL_RX_DATA_BLOCK(self, address, data):
#     def BSL_RX_DATA_BLOCK_FAST(self, address, data):
#     def BSL_TX_DATA_BLOCK(self, address, length):
#     def BSL_MASS_ERASE(self):
#     def BSL_ERASE_SEGMENT(self, address):
#     def BSL_LOAD_PC(self, address):
#     def BSL_RX_PASSWORD(self, password):
#     def BSL_VERSION(self):
#     def BSL_BUFFER_SIZE(self):
#     def BSL_LOCK_INFO(self):
#     def BSL_CRC_CHECK(self):
#     def detect_buffer_size(self):
#     def __init__(self):
#     def memory_read(self, address, length):
#     def memory_write(self, address, data):
#     def mass_erase(self):
#     def erase(self, address):
#     def execute(self, address):
#     def password(self, password):
#     def version(self):
#     def reset(self):
#         def bsl(self, cmd, message='', expect=None):


# Path: msp430/bsl5/hid.py
import sys
import os
import struct
import logging
import time
import pkgutil
import msp430.target
import msp430.memory
    import ctypes
    import Queue
    import glob
from msp430.bsl5 import bsl5
from optparse import OptionGroup
from cStringIO import StringIO
    from pywinusb import hid

        If the parameter "expect" is not None, "expect" bytes are expected in
        the answer, an exception is raised if the answer length does not match.
        If "expect" is None, the answer is just returned.

        Frame format:
        +------+-----+-----------+
        | 0x3f | len | D1 ... DN |
        +------+-----+-----------+
        """
        # first synchronize with slave
        self.logger.debug('Command 0x%02x (%d bytes)' % (cmd, 1+len(message)))
        #~ self.logger.debug('Command 0x%02x %s (%d bytes)' % (cmd, message.encode('hex'), 1+len(message)))
        txdata = bytearray(struct.pack('<BBB', 0x3f, 1+len(message), cmd) + message)
        txdata += b'\xac'*(64 - len(txdata)) # pad up to block size
        #~ self.logger.debug('Sending command: %r %d Bytes' % (txdata.encode('hex'), len(txdata)))
        # transmit command
        self.write_report(txdata)
        if receive_response:
            self.logger.debug('Reading answer...')
            report = self.read_report()
            self.logger.debug('report = %r' % report.encode('hex'))
            pi = report[0]
            if pi == '\x3f':
                length = ord(report[1])
                data = report[2:2+length]
                #~ if expect is not None and len(data) != expect:
                    #~ raise bsl5.BSL5Error('expected %d bytes, got %d bytes' % (expect, len(data)))
                return data
            else:
                if pi: raise bsl5.BSL5Error('received bad PI, expected 0x3f (got 0x%02x)' % (ord(pi),))

                raise bsl5.BSL5Error('received bad PI, expected 0x3f (got empty response)')
