# 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 serial
import struct
import logging
import time
import msp430.target
import msp430.memory
from msp430.bsl5 import bsl5
from optparse import OptionGroup

                    break
        if ans != BSL5_ACK:
            if ans: raise bsl5.BSL5Error('BSL reports error: %s (%s)' % (BSL5_UART_ERROR_CODES.get(ans, 'unknown error'), ans))
            raise bsl5.BSL5Error('No ACK received (timeout)')

    def bsl(self, cmd, message='', expect=None):
        self.bsl_header(cmd, message)
        if self.extra_timeout is None:
            head = self.serial.read(3)
        else:
            for timeout in range(self.extra_timeout):
                head = self.serial.read(3)
                if head:
                    break
        if len(head) != 3: raise bsl5.BSL5Timeout('timeout while reading answer (header)')
        pi, length = struct.unpack("<BH", head)
        if pi == 0x80:
            data = self.serial.read(length)
            if len(data) != length: raise bsl5.BSL5Timeout('timeout while reading answer (data)')
            crc_str = self.serial.read(2)
            if len(crc_str) != 2: raise bsl5.BSL5Timeout('timeout while reading answer (CRC)')
            crc = struct.unpack("<H", crc_str)
            crc_expected = reduce(crc_update, data, 0xffff)
            crc_expected = struct.pack('<H', crc_expected)
            if crc_str != crc_expected:
                raise bsl5.BSL5Exception('CRC error in answer')
            if expect is not None and length != expect:
                raise bsl5.BSL5Error('expected %d bytes, got %d bytes' % (expect, len(data)))
            return data
        else:

# 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/uart.py
import sys
import serial
import struct
import logging
import time
import msp430.target
import msp430.memory
from msp430.bsl5 import bsl5
from optparse import OptionGroup

                    break
        if ans != BSL5_ACK:
            if ans: raise bsl5.BSL5Error('BSL reports error: %s (%s)' % (BSL5_UART_ERROR_CODES.get(ans, 'unknown error'), ans))
            raise bsl5.BSL5Error('No ACK received (timeout)')

    def bsl(self, cmd, message='', expect=None):
        self.bsl_header(cmd, message)
        if self.extra_timeout is None:
            head = self.serial.read(3)
        else:
            for timeout in range(self.extra_timeout):
                head = self.serial.read(3)
                if head:
                    break
        if len(head) != 3: raise bsl5.BSL5Timeout('timeout while reading answer (header)')
        pi, length = struct.unpack("<BH", head)
        if pi == 0x80:
            data = self.serial.read(length)
            if len(data) != length: raise bsl5.BSL5Timeout('timeout while reading answer (data)')
            crc_str = self.serial.read(2)
            if len(crc_str) != 2: raise bsl5.BSL5Timeout('timeout while reading answer (CRC)')
            crc = struct.unpack("<H", crc_str)
            crc_expected = reduce(crc_update, data, 0xffff)
            crc_expected = struct.pack('<H', crc_expected)
            if crc_str != crc_expected:
                raise bsl5.BSL5Exception('CRC error in answer')
            if expect is not None and length != 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 0x80 (got 0x%02x)' % (pi))
