コード例 #1
0
    def open_connection(self):
        self.logger = logging.getLogger('BSL')
        self.open(
            self.options.port,
            parity=self.options.parity or serial.PARITY_EVEN,
            ignore_answer=self.options.ignore_answer,
        )
        self.control_delay = self.options.control_delay

        if self.options.test_on_tx:
            self.testOnTX = True

        if self.options.invert_test:
            self.invertTEST = True

        if self.options.invert_reset:
            self.invertRST = True

        if self.options.swap_reset_test:
            self.swapResetTest = True

        self.set_TEST(True)
        self.set_RST(True)

        if self.options.start_pattern:
            self.start_bsl()

        if self.options.do_mass_erase:
            self.logger.info("Mass erase...")
            self.extra_timeout = 20
            try:
                self.BSL_RX_PASSWORD('\xff' * 30 + '\0' * 2)
            except bsl5.BSL5Error:
                pass  # it will fail - that is our intention to trigger the erase
            time.sleep(1)
            self.BSL_RX_PASSWORD('\xff' * 32)
            # remove mass_erase from action list so that it is not done
            # twice
            self.extra_timeout = None
            self.remove_action(self.mass_erase)
        else:
            if self.options.password is not None:
                password = msp430.memory.load(self.options.password).get_range(
                    0xffe0, 0xffff)
                self.logger.info("Transmitting password: %s" %
                                 (password.encode('hex'), ))
                self.BSL_RX_PASSWORD(password)

        if self.options.speed is not None:
            try:
                self.set_baudrate(self.options.speed)
            except bsl5.BSL5Error:
                raise bsl5.BSL5Error(
                    "--speed option not supported by BSL on target")
コード例 #2
0
 def bsl(self, cmd, message='', expect=None):
     self.bsl_header(cmd, message)
     head = self.serial.read(3)
     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.BSLException('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))
         raise bsl5.BSL5Error('received bad PI, expected 0x80 (got empty response)')
コード例 #3
0
ファイル: uart.py プロジェクト: dpk14/MassSpecAutomator
    def bsl(self, cmd, message='', expect=None):
        """\
        Low level access to the serial communication.

        This function sends a command and waits until it receives an answer
        (including timeouts). It will return a string with the data part of
        the answer. In case of a failure read timeout or rejected commands by
        the slave, it will raise an exception.

        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:
        +-----+----+----+-----------+----+----+
        | HDR | LL | LH | D1 ... DN | CL | CH |
        +-----+----+----+-----------+----+----+
        """
        # first synchronize with slave
        self.logger.debug('Command 0x%02x %s' % (cmd, message.encode('hex')))
        # prepare command with checksum
        txdata = struct.pack('<BHB', 0x80, 1 + len(message), cmd) + message
        txdata += struct.pack('<H',
                              functools.reduce(crc_update, txdata,
                                               0xffff))  # append checksum
        #~ self.logger.debug('Sending command: %r' % (txdata.encode('hex'),))
        # transmit command
        self.serial.write(txdata)
        # wait for command answer
        if self.blindWrite:
            time.sleep(0.100)
            return
        if self.ignore_answer:
            return

        self.logger.debug('Reading answer...')
        if self.extra_timeout is None:
            ans = self.serial.read(1)
        else:
            for timeout in range(self.extra_timeout):
                ans = self.serial.read(1)
                if ans:
                    break
        if ans != BSL5_ACK:
            if ans:
                raise bsl5.BSL5Error(
                    'BSL reports error: %s' %
                    BSL5_UART_ERROR_CODES.get(ans, 'unknown error'))
            raise bsl5.BSL5Error('No ACK received (timeout)')

        head = self.serial.read(3)
        if len(head) != 3:
            raise bsl5.BSL5Timeout('timeout while reading answer (header)')
        pi, length = struct.unpack("<BH", head)
        if pi == '\x80':
            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 = functools.reduce(crc_update, head + data, 0xffff)
            if crc != crc_expected:
                raise bsl5.BSLException('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)' %
                    (ord(pi), ))
            raise bsl5.BSL5Error(
                'received bad PI, expected 0x80 (got empty response)')
コード例 #4
0
    def bsl(self, cmd, message='', expect=None, receive_response=True):
        """\
        Low level access to the HID communication.

        This function sends a command and waits until it receives an answer
        (including timeouts). It will return a string with the data part of
        the answer. The first byte will be the response code from the BSL

        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
        print("[bsl()] Control entered bsl()...")

        self.logger.debug('Command 0x%02x (%d bytes)' %
                          (cmd, 1 + len(message)))
        print('[bsl()] 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)))
        # FIXME SCC: In Python2, struct.pack() returned a string. In Python3, it returns a bytes object
        txdata = bytearray(
            struct.pack('<BBB', 0x3f, 1 + len(message), cmd).decode("utf8") +
            message,
            encoding="utf8")
        txdata += b'\xac' * (64 - len(txdata))  # pad up to block size

        print('Sending command: %r %d Bytes' % (txdata, len(txdata)))
        #~ 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...')
            print('Reading answer...')
            report = self.read_report()

            if sys.platform == 'darwin':

                self.logger.debug('report = %r' % report)
                print('report = %r' % report)

            else:
                # self.logger.debug('report = %r' % report.encode('hex'))
                self.logger.debug('report = %r' % report)
                print('report = %r' % report)

            pi = report[0]
            # XXX FIXME SCC This is a bad comparison because pi is of type bytes (and subscripting it is of type int) and the comparison is with a string literal (not the same!)
            # Fixing this by comparing with integer 0x3f instead
            if pi == 0x3f:
                length = 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)' % (pi, ))
                raise bsl5.BSL5Error(
                    'received bad PI, expected 0x3f (got empty response)')