def find_by_path(usb_path): p = [p for p in get_probes() if p.usb_path == usb_path] if not p: raise psdb.ProbeException('Path "%s" not found.' % usb_path) if len(p) > 1: raise psdb.ProbeException('Path "%s" is ambiguous.' % usb_path) return p[0]
def find_by_serial_number(serial_number): p = [p for p in get_probes() if p.serial_num == serial_number] if not p: raise psdb.ProbeException('Serial number "%s" not found.' % serial_number) if len(p) > 1: raise psdb.ProbeException('Serial number "%s" is ambiguous.' % serial_number) return p[0]
def find_default(serial_number=None, usb_path=None): if serial_number: return find_by_serial_number(serial_number) elif usb_path: return find_by_path(usb_path) elif get_probes(): if len(get_probes()) == 1: return get_probes()[0] print('Found probes:') dump_probes() raise psdb.ProbeException('Multiple probes found.') raise psdb.ProbeException('No compatible debug probe found.')
def _set_com_freq(self, freq_khz, is_jtag=False): ''' Sets the communication frequency to the highest supported frequency that doesn't exceed the requested one. Returns the actual frequency in kHz. ''' cmd = cdb.SetComFreq(freq_khz, is_jtag) try: return self._cmd_allow_retry(cmd) except errors.STLinkCmdException as e: if e.err != errors.COM_FREQ_TOO_LOW_ERROR: raise if is_jtag: raise psdb.ProbeException( 'Requested JTAG frequency %u kHz too low.' % freq_khz) raise psdb.ProbeException('Requested SWD frequency %u kHz too low; ' 'minimum is %u kHz.' % (freq_khz, self._swd_freqs_khz[-1]))
def _cmd_allow_retry(self, cmd, retries=10, delay=0.1): ''' Executes the CDB, retrying it if necessary based on the status code. Returns the decoded response if a response is expected. ''' for _ in range(retries): try: return self._exec_cdb(cmd) except errors.STLinkCmdException as e: if e.err not in (errors.SWD_AP_WAIT, errors.SWD_DP_WAIT): raise time.sleep(delay) raise psdb.ProbeException('Max retries exceeded!')
def __init__(self, usb_dev, name, usb_reset=False): super().__init__(name) self.usb_dev = usb_dev try: self.serial_num = usb_dev.serial_number except ValueError as e: if str(e) == 'The device has no langid': raise psdb.ProbeException('Device has no langid; ensure ' 'running as root!') self.usb_path = '%s:%s' % ( self.usb_dev.bus, '.'.join('%u' % n for n in self.usb_dev.port_numbers)) configurations = usb_dev.configurations() assert len(configurations) == 1 if usb_reset: usb_dev.reset() if (usb_dev.get_active_configuration().bConfigurationValue != configurations[0].bConfigurationValue): usb_dev.set_configuration(configurations[0].bConfigurationValue)
def set_tck_freq(self, freq_hz): ''' Sets the TCK to the nearest frequency that doesn't exceed the requested one. Returns the actual frequency in Hz. ''' assert self.features & stlink.FEATURE_SWD_SET_FREQ freq_map = [(4000000, 0), (1800000, 1), (1200000, 2), ( 950000, 3), ( 480000, 7), ( 240000, 15), ( 125000, 31), ( 100000, 40), ( 50000, 79), ( 25000, 158), ( 15000, 265), ( 5000, 798)] for f, d in freq_map: if freq_hz >= f: self._set_swdclk_divisor(d) return f raise psdb.ProbeException('Frequency %s too low!' % freq_hz)
def probe(self, verbose=False, connect_under_reset=False): ''' First discovers which APs are attached to the debug probe and then performs component topology detection on each AP. Finally, we attempt to match the resulting components to a known target. After components have been matched, the target must be halted before it is further probed. When we return, the target remains in the halted state and Target.resume() must be invoked if it is to continue running. If connect_under_reset is True, the SRST line will be asserted and then both the connection process and the component probing will take place while the MCU is held in SRST. Reset vector catch will then be configured, SRST will be released and the MCU will then end up halted right in the reset vector and we return If connect_under_reset is False, the SRST line will be deasserted (in case it had been previously asserted - we assume that the MCU does not support probing under SRST) and then component probing will take place *while the MCU is running*. Finally, the MCU will be halted wherever it happens to be running and we return. ''' if connect_under_reset: self.assert_srst() else: self.deassert_srst() self.connect() dpver = ((self.dpidr & 0x0000F000) >> 12) if dpver == 1: self._probe_dp_v1(verbose=verbose) elif dpver == 2: self._probe_dp_v2(verbose=verbose) else: raise psdb.ProbeException('Unsupported DP version %u (0x%08X)' % ( dpver, self.dpidr)) psdb.targets.pre_probe(self, verbose) self.cpus = [] for _, ap in self.aps.items(): ap.base_component = ap.probe_components(verbose=verbose) if connect_under_reset: for c in self.cpus: c.enable_reset_vector_catch() self.deassert_srst() for c in self.cpus: c.inval_halted_state() while not c.is_halted(): pass for c in self.cpus: c.disable_reset_vector_catch() else: self.halt() self.target = psdb.targets.probe(self) assert self.target if verbose: print(' Identified target %s' % self.target) return self.target