Пример #1
0
    def _coresight_reg_read(self, access_port: bool = True, addr: int = 0) -> int:
        """Read coresight register over PyLink interface.

        The PyLink read coresight register function for SPSDK library to support various DEBUG PROBES.
        :param access_port: if True, the Access Port (AP) register will be read(defau1lt), otherwise the Debug Port
        :param addr: the register address
        :return: The read value of addressed register (4 bytes)
        :raises DebugProbeTransferError: The IO operation failed
        :raises DebugProbeNotOpenError: The PyLink probe is NOT opened

        """
        if self.pylink is None:
            raise DebugProbeNotOpenError("The PyLink debug probe is not opened yet")

        try:
            if not self.use_coresight_rw:
                request = swd.ReadRequest(addr // 4, ap=access_port)
                response = request.send(self.pylink)
                if access_port:
                    sleep(0.1)  #TODO Check if this delay is necessary
                    request2 = swd.ReadRequest(3, ap=False)
                    response2 = request2.send(self.pylink)
                    return response2.data

                return response.data
            return self.pylink.coresight_read(reg=addr // 4, ap=access_port)
        except JLinkException as exc:
            raise DebugProbeTransferError(f"The Coresight read operation failed({str(exc)}).")
Пример #2
0
    def coresight_reg_read(self,
                           access_port: bool = True,
                           addr: int = 0) -> int:
        """Read coresight register over PyLink interface.

        The PyLink read coresight register function for SPSDK library to support various DEBUG PROBES.

        :param access_port: if True, the Access Port (AP) register will be read(default), otherwise the Debug Port
        :param addr: the register address
        :return: The read value of addressed register (4 bytes)
        :raises SPSDKDebugProbeTransferError: The IO operation failed
        :raises SPSDKDebugProbeNotOpenError: The PyLink probe is NOT opened

        """
        if self.pylink is None:
            raise SPSDKDebugProbeNotOpenError(
                "The PyLink debug probe is not opened yet")

        try:
            if access_port:
                self._select_ap(addr)
                addr = addr & 0x0F
            else:
                self.last_accessed_ap = -1

            if not self.use_coresight_rw:
                request = swd.ReadRequest(addr // 4, ap=access_port)
                response = request.send(self.pylink)
                if access_port:
                    sleep(0.1)
                    request2 = swd.ReadRequest(3, ap=False)
                    response2 = request2.send(self.pylink)
                    return response2.data

                return response.data
            return self.pylink.coresight_read(reg=addr // 4, ap=access_port)
        except (JLinkException, ValueError, TypeError) as exc:
            # In case of transaction error reconfigure and initialize the JLink
            self._reinit_jlink_target()
            raise SPSDKDebugProbeTransferError(
                f"The Coresight read operation failed({str(exc)}).") from exc
Пример #3
0
    def read_reg(self,
                 addr: int,
                 now: bool = True,
                 requiresDelay: bool = False) -> bytes:
        """Read register.

        :param addr: the register index
        :param now: bool value
        :param requiresDelay: if the delay is required, there is sleep for 0.1 second
        :return:
        """
        if not self.use_coresight_rw:
            request = swd.ReadRequest(addr // 4, ap=True)
            response = request.send(self.jlink)
            if requiresDelay is True:
                sleep(0.1)
            request2 = swd.ReadRequest(3, ap=False)
            response2 = request2.send(self.jlink)
            return response2.data
        else:
            return self.jlink.coresight_read(reg=addr // 4, ap=True)
Пример #4
0
    def test_swd_read_request_initialize(self):
        """Tests creating a SWD Read Request.

        When a SWD Read Request is created, there is a structure specifying
        what the underlying bits should look like.  This test verifies a
        number of different valid bitfields.

        Args:
          self (TestSerialWireDebug): the `TestSerialWireDebug` instance

        Returns:
          `None`
        """
        values = [165, 141, 149, 189, 165]
        for (index, value) in enumerate(values):
            request = swd.ReadRequest(index, ap=False)
            self.assertEqual(value, request.value)

        values = [135, 175, 183, 159, 135]
        for (index, value) in enumerate(values):
            request = swd.ReadRequest(index, ap=True)
            self.assertEqual(value, request.value)
Пример #5
0
    def test_swd_read_request_send_ack_parity_mismatch(self):
        """Tests sending a SWD Request that is ACK'd, but the parity is wrong.

        When a SWD Read Request reads data from the target, their is a parity
        field that is set, and can be is to verify that the data is valid.  In
        this test, the parity check fails.

        Args:
          self (TestSerialWireDebug): the `TestSerialWireDebug` instance

        Returns:
          `None`
        """
        request = swd.ReadRequest(0, True)

        ack = 1
        status = swd.Response.STATUS_ACK
        data = 3

        mock_jlink = mock.Mock()
        mock_jlink.swd_write.return_value = ack
        mock_jlink.swd_read8.return_value = status
        mock_jlink.swd_read32.return_value = data

        response = request.send(mock_jlink)

        self.assertFalse(response.ack())
        self.assertTrue(response.invalid())

        self.assertEqual(2, mock_jlink.swd_write8.call_count)
        mock_jlink.swd_write8.assert_any_call(0xFF,
                                              request.value)  # data command
        mock_jlink.swd_write8.assert_any_call(0xFC, 0x0)  # status command

        self.assertEqual(1, mock_jlink.swd_write32.call_count)
        mock_jlink.swd_write32.assert_any_call(0x0, 0x0)

        self.assertEqual(1, mock_jlink.swd_write.call_count)
        mock_jlink.swd_write.assert_any_call(0x0, 0x0, 3)  # ack

        self.assertEqual(2, mock_jlink.swd_read8.call_count)
        mock_jlink.swd_read8.assert_any_call(ack)  # status read
        mock_jlink.swd_read8.assert_any_call(ack + 35)  # parity check

        self.assertEqual(1, mock_jlink.swd_read32.call_count)
        mock_jlink.swd_read32.assert_any_call(ack + 3)  # data read
Пример #6
0
    def test_swd_read_request_send_ack(self):
        """Tests sending a SWD Read Request that is ACK'd.

        Args:
          self (TestSerialWireDebug): the `TestSerialWireDebug` instance

        Returns:
          `None`
        """
        request = swd.ReadRequest(0, True)

        ack = 1
        status = swd.Response.STATUS_ACK
        data = 1

        mock_jlink = mock.Mock()
        mock_jlink.swd_write.return_value = ack
        mock_jlink.swd_read8.return_value = status
        mock_jlink.swd_read32.return_value = data

        response = request.send(mock_jlink)

        self.assertTrue(response.ack())

        self.assertEqual(2, mock_jlink.swd_write8.call_count)
        mock_jlink.swd_write8.assert_any_call(0xFF,
                                              request.value)  # data command
        mock_jlink.swd_write8.assert_any_call(0xFC, 0x0)  # status command

        self.assertEqual(1, mock_jlink.swd_write32.call_count)
        mock_jlink.swd_write32.assert_any_call(0x0, 0x0)

        self.assertEqual(1, mock_jlink.swd_write.call_count)
        mock_jlink.swd_write.assert_any_call(0x0, 0x0, 3)  # ack

        self.assertEqual(2, mock_jlink.swd_read8.call_count)
        mock_jlink.swd_read8.assert_any_call(ack)  # status read
        mock_jlink.swd_read8.assert_any_call(ack + 35)  # parity check

        self.assertEqual(1, mock_jlink.swd_read32.call_count)
        mock_jlink.swd_read32.assert_any_call(ack + 3)  # data read