You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Is a python module for debugging microcontrollers with SWD using ST-Link/V2 (/V2-1) or V3 debugger.
This package also contain small command line tool.
Goal
Is to create python module for access debugging interface on MCU with SWD interface.
Main purpose of python module is to create automated functional and hardware tests from simple python scripts and without special firmware for microcontroller.
Compatibility
OS
PYSWD will work on Linux, Mac and Windows.
Python
Python 3.7+
Dependencies
pyusb - prefer latest version from github, especially on Windows platgorm
serial_no: serial number of connected USB ST-Link debugger (optional). Serial number can be also part from begin or end, if more devices are detected then it stops with error
>>>importswd>>>dev=swd.Swd()
ST-Link version
property with ST-Link version
Return:
instance of StlinkVersion
>>>dev.get_version().str'ST-Link/V2 V2J27S6'
Target voltage
Get target voltage measured by ST-Link
Return:
float target voltage in volts
>>>dev.get_target_voltage()3.21
ID code
Get MCU ID code
Return:
32bit unsigned with ID code
>>>hex(dev.get_idcode())'0xbb11477'
Get memory register
get_mem32(address)
Arguments:
address: address in memory, must be aligned to 32bits
Return:
32bit unsigned data from memory
>>>hex(dev.get_mem32(0x08000000))'0x20001000'
Set memory register
set_mem32(address, data)
Arguments:
address: address in memory, must be aligned to 32bits
Some MCUs (with STM32H5 series as an example) could have multiple independent APs (debug access ports). In such cases,you will need to specify which AP debugger should use:
swd.open_ap(1)swd.default_ap=1# continue using as usual:cm=CortexM(swd)print(cm.get_reg_all())
Note that you should use onlyCortexM high-level functions, as these functions will dynamically select between ST-Linknative implementation (which is faster, but supports only AP0) and software emulation via memory accesses.SWD classexposes ST-Link native implementation directly, regardless of default AP selected.
To dynamically detect which MCU is attached, you could use IDCODE and DBGMCU registers. Both methods are AP-independent,as DBGMCU register block is available on AP0 as well.
Python application
Simple tool for access MCU debugging features from command line. Is installed together with python module.
-h, --help show this help message and exit-V, --version show program's version number and exit-q, --quite quite output-d, --debug increase debug output-i, --info increase info output-v, --verbose increase verbose output-f FREQ, --freq FREQ set SWD frequency-s SERIAL, --serial SERIAL select ST-Link by serial number (enough is part of serial number: begin or end
List of available actions:
dump8:{addr}[:{size}] print content of memory 8 bit register or dump dump16:{addr}[:{size}] print content of memory 16 bit register or dump dump32:{addr}[:{size}] print content of memory 32 bit register or dump dump:{addr}[:{size}] print content of memory 32 bit register or 8 bit dump set8:{addr}:{data}[:{data}..] set 8 bit memory set16:{addr}:{data}[:{data}..] set 16 bit memory set32:{addr}:{data}[:{data}..] set 32 bit memory set:{addr}:{data}[:{data}..] set 32 bit memory register or 8 bit memory area fill8:{addr}:{size}:{pattern} fill memory with 8 bit pattern reg:all print all core register reg:{reg} print content of core register reg:{reg}:{data} set core register reset[:halt] reset core or halt after reset run[:nodebug] run core step[:{n}] step core (n-times) halt halt core sleep:{seconds} sleep (float) - insert delay between commands
(numerical values can be in different formats, like: 42, 0x2a, 0o52, 0b101010, 32K, 1M, ..)