Best Python code snippet using pyresttest_python
cmsis_dap_probe.py
Source:cmsis_dap_probe.py  
...67    def get_all_connected_probes(cls):68        try:69            return [cls(dev) for dev in DAPAccess.get_connected_devices()]70        except DAPAccess.Error as exc:71            six.raise_from(cls._convert_exception(exc), exc)72    73    @classmethod74    def get_probe_with_id(cls, unique_id):75        try:76            return cls(DAPAccess(unique_id))77        except DAPAccess.Error as exc:78            six.raise_from(cls._convert_exception(exc), exc)79    def __init__(self, device):80        self._link = device81        self._supported_protocols = None82        self._protocol = None83        self._is_open = False84        self._dp_select = -185        86    @property87    def description(self):88        try:89            board_id = self.unique_id[0:4]90            board_info = BOARD_ID_TO_INFO[board_id]91        except KeyError:92            return self.vendor_name + " " + self.product_name93        else:94            return "{0} [{1}]".format(board_info.name, board_info.target)95    96    @property97    def vendor_name(self):98        return self._link.vendor_name99    100    @property101    def product_name(self):102        return self._link.product_name103    ## @brief Only valid after opening.104    @property105    def supported_wire_protocols(self):106        return self._supported_protocols107    @property108    def unique_id(self):109        return self._link.get_unique_id()110    @property111    def wire_protocol(self):112        return self._protocol113    114    @property115    def is_open(self):116        return self._is_open117    def create_associated_board(self, session):118        # Only support associated Mbed boards for DAPLink firmware. We can't assume other119        # CMSIS-DAP firmware is using the same serial number format, so we cannot reliably120        # extract the board ID.121        if self._link.vidpid == self.DAPLINK_VIDPID:122            return MbedBoard(session)123        else:124            return None125    126    def open(self):127        try:128            self._link.open()129            self._is_open = True130            self._link.set_deferred_transfer(True)131        132            # Read CMSIS-DAP capabilities133            self._capabilities = self._link.identify(DAPAccess.ID.CAPABILITIES)134            self._supported_protocols = [DebugProbe.Protocol.DEFAULT]135            if self._capabilities & self.SWD_CAPABILITY_MASK:136                self._supported_protocols.append(DebugProbe.Protocol.SWD)137            if self._capabilities & self.JTAG_CAPABILITY_MASK:138                self._supported_protocols.append(DebugProbe.Protocol.JTAG)139        except DAPAccess.Error as exc:140            six.raise_from(self._convert_exception(exc), exc)141    142    def close(self):143        try:144            self._link.close()145            self._is_open = False146        except DAPAccess.Error as exc:147            six.raise_from(self._convert_exception(exc), exc)148    # ------------------------------------------- #149    #          Target control functions150    # ------------------------------------------- #151    def connect(self, protocol=None):152        """Initialize DAP IO pins for JTAG or SWD"""153        # Convert protocol to port enum.154        if protocol is not None:155            port = self.PORT_MAP[protocol]156        else:157            port = DAPAccess.PORT.DEFAULT158        159        try:160            self._link.connect(port)161        except DAPAccess.Error as exc:162            six.raise_from(self._convert_exception(exc), exc)163        164        # Read the current mode and save it.165        actualMode = self._link.get_swj_mode()166        self._protocol = self.PORT_MAP[actualMode]167        168        self._invalidate_cached_registers()169    # TODO remove170    def swj_sequence(self):171        """Send sequence to activate JTAG or SWD on the target"""172        try:173            self._link.swj_sequence()174        except DAPAccess.Error as exc:175            six.raise_from(self._convert_exception(exc), exc)176    def disconnect(self):177        """Deinitialize the DAP I/O pins"""178        try:179            self._link.disconnect()180            self._protocol = None181            self._invalidate_cached_registers()182        except DAPAccess.Error as exc:183            six.raise_from(self._convert_exception(exc), exc)184    def set_clock(self, frequency):185        """Set the frequency for JTAG and SWD in Hz186        This function is safe to call before connect is called.187        """188        try:189            self._link.set_clock(frequency)190        except DAPAccess.Error as exc:191            six.raise_from(self._convert_exception(exc), exc)192    def reset(self):193        """Reset the target"""194        try:195            self._invalidate_cached_registers()196            self._link.reset()197        except DAPAccess.Error as exc:198            six.raise_from(self._convert_exception(exc), exc)199    def assert_reset(self, asserted):200        """Assert or de-assert target reset line"""201        try:202            self._invalidate_cached_registers()203            self._link.assert_reset(asserted)204        except DAPAccess.Error as exc:205            six.raise_from(self._convert_exception(exc), exc)206    207    def is_reset_asserted(self):208        """Returns True if the target reset line is asserted or False if de-asserted"""209        try:210            return self._link.is_reset_asserted()211        except DAPAccess.Error as exc:212            six.raise_from(self._convert_exception(exc), exc)213    def flush(self):214        """Write out all unsent commands"""215        try:216            self._link.flush()217        except DAPAccess.Error as exc:218            six.raise_from(self._convert_exception(exc), exc)219    # ------------------------------------------- #220    #          DAP Access functions221    # ------------------------------------------- #222    ## @brief Read a DP register.223    #224    # @param self225    # @param addr Integer register address being one of (0x0, 0x4, 0x8, 0xC).226    # @param now227    #228    # @todo Handle auto DPBANKSEL.229    def read_dp(self, addr, now=True):230        reg_id = self.REG_ADDR_TO_ID_MAP[self.DP, addr]231        232        try:233            result = self._link.read_reg(reg_id, now=now)234        except DAPAccess.Error as error:235            self._invalidate_cached_registers()236            six.raise_from(self._convert_exception(error), error)237        # Read callback returned for async reads.238        def read_dp_result_callback():239            try:240                return result()241            except DAPAccess.Error as error:242                self._invalidate_cached_registers()243                six.raise_from(self._convert_exception(error), error)244        return result if now else read_dp_result_callback245    def write_dp(self, addr, data):246        reg_id = self.REG_ADDR_TO_ID_MAP[self.DP, addr]247        248        # Skip writing DP SELECT register if its value is not changing.249        if addr == self.DP_SELECT:250            if data == self._dp_select:251                return252            self._dp_select = data253        # Write the DP register.254        try:255            self._link.write_reg(reg_id, data)256        except DAPAccess.Error as error:257            self._invalidate_cached_registers()258            six.raise_from(self._convert_exception(error), error)259        return True260    def read_ap(self, addr, now=True):261        assert type(addr) in (six.integer_types)262        ap_reg = self.REG_ADDR_TO_ID_MAP[self.AP, (addr & self.A32)]263        try:264            self.write_dp(self.DP_SELECT, addr & self.APSEL_APBANKSEL)265            result = self._link.read_reg(ap_reg, now=now)266        except DAPAccess.Error as error:267            self._invalidate_cached_registers()268            six.raise_from(self._convert_exception(error), error)269        # Read callback returned for async reads.270        def read_ap_result_callback():271            try:272                return result()273            except DAPAccess.Error as error:274                self._invalidate_cached_registers()275                six.raise_from(self._convert_exception(error), error)276        return result if now else read_ap_result_callback277    def write_ap(self, addr, data):278        assert type(addr) in (six.integer_types)279        ap_reg = self.REG_ADDR_TO_ID_MAP[self.AP, (addr & self.A32)]280        # Select the AP and bank.281        self.write_dp(self.DP_SELECT, addr & self.APSEL_APBANKSEL)282        # Perform the AP register write.283        try:284            self._link.write_reg(ap_reg, data)285        except DAPAccess.Error as error:286            self._invalidate_cached_registers()287            six.raise_from(self._convert_exception(error), error)288        return True289    def read_ap_multiple(self, addr, count=1, now=True):290        assert type(addr) in (six.integer_types)291        ap_reg = self.REG_ADDR_TO_ID_MAP[self.AP, (addr & self.A32)]292        293        try:294            # Select the AP and bank.295            self.write_dp(self.DP_SELECT, addr & self.APSEL_APBANKSEL)296            297            result = self._link.reg_read_repeat(count, ap_reg, dap_index=0, now=now)298        except DAPAccess.Error as exc:299            self._invalidate_cached_registers()300            six.raise_from(self._convert_exception(exc), exc)301        # Need to wrap the deferred callback to convert exceptions.302        def read_ap_repeat_callback():303            try:304                return result()305            except DAPAccess.Error as exc:306                self._invalidate_cached_registers()307                six.raise_from(self._convert_exception(exc), exc)308        return result if now else read_ap_repeat_callback309    def write_ap_multiple(self, addr, values):310        assert type(addr) in (six.integer_types)311        ap_reg = self.REG_ADDR_TO_ID_MAP[self.AP, (addr & self.A32)]312        313        try:314            # Select the AP and bank.315            self.write_dp(self.DP_SELECT, addr & self.APSEL_APBANKSEL)316            317            return self._link.reg_write_repeat(len(values), ap_reg, values, dap_index=0)318        except DAPAccess.Error as exc:319            self._invalidate_cached_registers()320            six.raise_from(self._convert_exception(exc), exc)321    322    # ------------------------------------------- #323    #          SWO functions324    # ------------------------------------------- #325    def has_swo(self):326        """! @brief Returns bool indicating whether the link supports SWO."""327        try:328            return self._link.has_swo()329        except DAPAccess.Error as exc:330            six.raise_from(self._convert_exception(exc), exc)331    def swo_start(self, baudrate):332        """! @brief Start receiving SWO data at the given baudrate."""333        try:334            self._link.swo_configure(True, baudrate)335            self._link.swo_control(True)336        except DAPAccess.Error as exc:337            six.raise_from(self._convert_exception(exc), exc)338    def swo_stop(self):339        """! @brief Stop receiving SWO data."""340        try:341            self._link.swo_configure(False, 0)342        except DAPAccess.Error as exc:343            six.raise_from(self._convert_exception(exc), exc)344    def swo_read(self):345        """! @brief Read buffered SWO data from the target.346        347        @eturn Bytearray of the received data.348        """349        try:350            return self._link.swo_read()351        except DAPAccess.Error as exc:352            six.raise_from(self._convert_exception(exc), exc)353    def _invalidate_cached_registers(self):354        # Invalidate cached DP SELECT register.355        self._dp_select = -1356    @staticmethod357    def _convert_exception(exc):358        if isinstance(exc, DAPAccess.TransferFaultError):359            return exceptions.TransferFaultError()360        elif isinstance(exc, DAPAccess.TransferTimeoutError):361            return exceptions.TransferTimeoutError()362        elif isinstance(exc, DAPAccess.TransferError):363            return exceptions.TransferError()364        elif isinstance(exc, (DAPAccess.DeviceError, DAPAccess.CommandError)):365            return exceptions.ProbeError(str(exc))366        elif isinstance(exc, DAPAccess.Error):...stlink_probe.py
Source:stlink_probe.py  
...29    def get_all_connected_probes(cls):30        try:31            return [cls(dev) for dev in usb.STLinkUSBInterface.get_all_connected_devices()]32        except STLinkException as exc:33            six.raise_from(cls._convert_exception(exc), exc)34    35    @classmethod36    def get_probe_with_id(cls, unique_id):37        try:38            for dev in usb.STLinkUSBInterface.get_all_connected_devices():39                if dev.serial_number == unique_id:40                    return cls(usb.STLinkUSBInterface(unique_id))41            else:42                return None43        except STLinkException as exc:44            six.raise_from(cls._convert_exception(exc), exc)45    def __init__(self, device):46        self._link = stlink.STLink(device)47        self._is_open = False48        self._is_connected = False49        self._nreset_state = False50        self._memory_interfaces = {}51        52    @property53    def description(self):54        return self.product_name55    56    @property57    def vendor_name(self):58        return self._link.vendor_name59    60    @property61    def product_name(self):62        return self._link.product_name63    ## @brief Only valid after opening.64    @property65    def supported_wire_protocols(self):66        return [DebugProbe.Protocol.DEFAULT, DebugProbe.Protocol.SWD, DebugProbe.Protocol.JTAG]67    @property68    def unique_id(self):69        return self._link.serial_number70    @property71    def wire_protocol(self):72        return DebugProbe.Protocol.SWD if self._is_connected else None73    74    @property75    def is_open(self):76        return self._is_open77    78    def open(self):79        try:80            self._link.open()81            self._is_open = True82        except STLinkException as exc:83            six.raise_from(self._convert_exception(exc), exc)84    85    def close(self):86        try:87            self._link.close()88            self._is_open = False89        except STLinkException as exc:90            six.raise_from(self._convert_exception(exc), exc)91    # ------------------------------------------- #92    #          Target control functions93    # ------------------------------------------- #94    def connect(self, protocol=None):95        """Initialize DAP IO pins for JTAG or SWD"""96        try:97            self._link.enter_debug(stlink.STLink.Protocol.SWD)98            self._is_connected = True99        except STLinkException as exc:100            six.raise_from(self._convert_exception(exc), exc)101    # TODO remove102    def swj_sequence(self):103        """Send sequence to activate JTAG or SWD on the target"""104        pass105    def disconnect(self):106        """Deinitialize the DAP I/O pins"""107        try:108            # TODO Close the APs. When this is attempted, we get an undocumented 0x1d error. Doesn't109            #      seem to be necessary, anyway.110            self._memory_interfaces = {}111            112            self._link.enter_idle()113            self._is_connected = False114        except STLinkException as exc:115            six.raise_from(self._convert_exception(exc), exc)116    def set_clock(self, frequency):117        """Set the frequency for JTAG and SWD in Hz118        This function is safe to call before connect is called.119        """120        try:121            self._link.set_swd_frequency(frequency)122        except STLinkException as exc:123            six.raise_from(self._convert_exception(exc), exc)124    def reset(self):125        """Reset the target"""126        try:127            self._link.target_reset()128        except STLinkException as exc:129            six.raise_from(self._convert_exception(exc), exc)130    def assert_reset(self, asserted):131        """Assert or de-assert target reset line"""132        try:133            self._link.drive_nreset(asserted)134            self._nreset_state = asserted135        except STLinkException as exc:136            six.raise_from(self._convert_exception(exc), exc)137    138    def is_reset_asserted(self):139        """Returns True if the target reset line is asserted or False if de-asserted"""140        return self._nreset_state141    def flush(self):142        """Write out all unsent commands"""143        pass144    # ------------------------------------------- #145    #          DAP Access functions146    # ------------------------------------------- #147    148    def read_dp(self, addr, now=True):149        try:150            result = self._link.read_dap_register(stlink.STLink.DP_PORT, addr)151        except STLinkException as exc:152            six.raise_from(self._convert_exception(exc), exc)153        154        def read_dp_result_callback():155            return result156        157        return result if now else read_dp_result_callback158    def write_dp(self, addr, data):159        try:160            result = self._link.write_dap_register(stlink.STLink.DP_PORT, addr, data)161        except STLinkException as exc:162            six.raise_from(self._convert_exception(exc), exc)163    def read_ap(self, addr, now=True):164        try:165            apsel = (addr & self.APSEL) >> self.APSEL_SHIFT166            result = self._link.read_dap_register(apsel, addr & 0xffff)167        except STLinkException as exc:168            six.raise_from(self._convert_exception(exc), exc)169        170        def read_ap_result_callback():171            return result172        173        return result if now else read_ap_result_callback174    def write_ap(self, addr, data):175        try:176            apsel = (addr & self.APSEL) >> self.APSEL_SHIFT177            result = self._link.write_dap_register(apsel, addr & 0xffff, data)178        except STLinkException as exc:179            six.raise_from(self._convert_exception(exc), exc)180    def read_ap_multiple(self, addr, count=1, now=True):181        results = [self.read_ap(addr, now=True) for n in range(count)]182        183        def read_ap_multiple_result_callback():184            return result185        186        return results if now else read_ap_multiple_result_callback187    def write_ap_multiple(self, addr, values):188        for v in values:189            self.write_ap(addr, v)190    def get_memory_interface_for_ap(self, apsel):191        assert self._is_connected192        if apsel not in self._memory_interfaces:193            self._link.open_ap(apsel)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
