def test_swd_write_request_send(self): """Tests sending a SWD Read Request. Args: self (TestSerialWireDebug): the `TestSerialWireDebug` instance Returns: `None` """ data = 2 parity = pylink.util.calculate_parity(data) request = swd.WriteRequest(0, True, data) ack = 2 mock_jlink = mock.Mock() mock_jlink.swd_write.return_value = ack mock_jlink.swd_read8.return_value = 1 response = request.send(mock_jlink) self.assertTrue(response.ack()) self.assertEqual(2, mock_jlink.swd_write.call_count) mock_jlink.swd_write.assert_any_call(0x0, 0x0, 3) # Ack mock_jlink.swd_write.assert_any_call(0x0, 0x0, 2) # Turnaround self.assertEqual(2, mock_jlink.swd_write8.call_count) mock_jlink.swd_write8.assert_any_call(0xFF, request.value) mock_jlink.swd_write8.assert_any_call(0xFF, parity) self.assertEqual(1, mock_jlink.swd_write32.call_count) mock_jlink.swd_write32.assert_called_once_with(0xFFFFFFFF, data) self.assertEqual(1, mock_jlink.swd_read8.call_count) mock_jlink.swd_read8.assert_called_once_with(ack)
def __init__(self, apnumber: int, serial_no: int = None, ip_addr: str = None) -> None: """Initialize. :param apnumber: ap number :param serial_no: serial number :param ip_addr: ip address """ self.apnumber = apnumber self.jlink = pylink.JLink() self.jlink.open(serial_no=serial_no, ip_addr=ip_addr) self.jlink.set_tif(pylink.enums.JLinkInterfaces.SWD) # self.jlink.set_speed(4000) # MUST BE UNUSED self.jlink.connect("CORTEX-M33", verbose=True) self.jlink.coresight_configure() # Use coresight_read/write API - True (default) # or the original swd interface- False (this did not work properly) self.use_coresight_rw = True # Select ISP - AP if not self.use_coresight_rw: request = swd.WriteRequest(2, data=(0x000000F0 | (apnumber << 24)), ap=False) response = request.send(self.jlink) assert response.ack(), "No ack from JLink" else: self.jlink.coresight_write(reg=2, data=(0x000000F0 | (apnumber << 24)), ap=False) if not self.use_coresight_rw: request = swd.WriteRequest(2, data=(0x00000000 | (apnumber << 24)), ap=False) response = request.send(self.jlink) assert response.ack(), "No ack from JLink" else: self.jlink.coresight_write(reg=2, data=(0x00000000 | (apnumber << 24)), ap=False)
def write_reg(self, addr: int, value: int) -> None: """Write register. :param addr: the register index :param value: which will be written to register """ if not self.use_coresight_rw: request = swd.WriteRequest(addr // 4, data=value, ap=True) response = request.send(self.jlink) assert response.ack(), "No ack from JLink" else: self.jlink.coresight_write(reg=addr // 4, data=value, ap=True)
def test_swd_write_request_initialize(self): """Tests creating a SWD Write Request. When a SWD Write 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` """ data = 4 values = [129, 169, 177, 153] for (index, value) in enumerate(values): request = swd.WriteRequest(index, data=data, ap=False) self.assertEqual(value, request.value) values = [163, 139, 147, 187] for (index, value) in enumerate(values): request = swd.WriteRequest(index, data=data, ap=True) self.assertEqual(value, request.value)
def coresight_reg_write(self, access_port: bool = True, addr: int = 0, data: int = 0) -> None: """Write coresight register over PyLink interface. The PyLink write coresight register function for SPSDK library to support various DEBUG PROBES. :param access_port: if True, the Access Port (AP) register will be write(default), otherwise the Debug Port :param addr: the register address :param data: the data to be written into register :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.WriteRequest(addr // 4, data=data, ap=access_port) response = request.send(self.pylink) if not response.ack(): raise SPSDKDebugProbeTransferError("No ack from JLink") else: self.pylink.coresight_write(reg=addr // 4, data=data, 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 write operation failed({str(exc)}).") from exc
def coresight_reg_write(self, access_port: bool = True, addr: int = 0, data: int = 0) -> None: """Write coresight register over PyLink interface. The PyLink write coresight register function for SPSDK library to support various DEBUG PROBES. :param access_port: if True, the Access Port (AP) register will be write(default), otherwise the Debug Port :param addr: the register address :param data: the data to be written into register :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 access_port: req_ap = (self.APSEL & addr) >> self.APSEL_SHIFT if self.last_accessed_ap != req_ap: self._select_ap(req_ap) self.last_accessed_ap = req_ap if not self.use_coresight_rw: request = swd.WriteRequest(addr // 4, data=data, ap=access_port) response = request.send(self.pylink) if not response.ack(): raise DebugProbeTransferError("No ack from JLink") else: self.pylink.coresight_write(reg=addr // 4, data=data, ap=access_port) except JLinkException as exc: raise DebugProbeTransferError( f"The Coresight write operation failed({str(exc)}).")