Is a python module for debugging microcontrollers with SWD using ST-Link/V2 (/V2-1) debugger.
This package also contain small command line tool.
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.
PYSWD will work on Linux, Mac and Windows.
Python 3.4+ (Development is under python 3.6)
pip3 install .
pip3 install --upgrade .
pip3 uninstall pyswd
swd.Swd(swd_frequency=1800000, logger=None)
- swd_frequency: SWD communication frequency
- logger: logging interface (optional)
>>> import swd
>>> dev = swd.Swd()
property with ST-Link version
instance of StlinkVersion
>>> dev.version.str
'ST-Link/V2 V2J27S6'
Get target voltage measured by ST-Link
float target voltage in volts
>>> dev.get_target_voltage()
3.21
Get MCU ID code
32bit unsigned with ID code
>>> hex(dev.get_idcode())
'0xbb11477'
get_mem32(address)
- address: address in memory, must be aligned to 32bits
32bit unsigned data from memory
>>> hex(dev.get_mem32(0x08000000))
'0x20001000'
set_mem32(address, data)
- address: address in memory, must be aligned to 32bits
- data: 32bit unsigned data
>>> dev.set_mem32(0x20000200, 0x12345678)
>>> hex(dev.get_mem32(0x20000200))
'0x12345678
read_mem(address, size)
- address: address in memory
- size: number of bytes to read from memory
iterable of read data
>>> data = dev.read_mem(0x08000000, 16)
>>> ' '.join(['%02x' % d for d in data])
'00 10 00 20 45 00 00 08 41 00 00 08 41 00 00 08'
write_mem(address, data)
- address: address in memory
- data: list or iterable of bytes whic will be stored into memory
>>> dev.write_mem(0x20000100, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> data = dev.read_mem(0x20000100, 15)
>>> ' '.join(['%02x' % d for d in data])
'01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f'
write_mem(address, pattern, size)
- address: address in memory
- pattern: list or iterable of bytes whic will be stored into memory
- size: number of bytes to fill memory
>>> dev.fill_mem(0x20000300, [5, 6, 7], 20)
>>> data = dev.read_mem(0x20000300, 20)
>>> ' '.join(['%02x' % d for d in data])
'05 06 07 05 06 07 05 06 07 05 06 07 05 06 07 05 06 07 05 06'
get_reg(register)
On CortexM platform this will work only if program is halted
- register: is numeric coded register (e.g. 0: R0, 1: R1, ...)
32bit unsigned data
>>> hex(dev.get_reg(1))
'0x0800012e'
get_reg(register)
On CortexM platform this will work only if program is halted
- register: is numeric coded register (e.g. 0: R0, 1: R1, ...)
- data: 32bit unsigned data
>>> hex(dev.get_reg(1))
'0x0800012e'
Simple tool for access MCU debugging features from command line. Is installed together with python module.
$ pyswd --help
pyswd [-h] [-V] [-q] [-d] [-i] [-v] [-f FREQ] [action [action ...]]
action actions will be processed sequentially
-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
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
sleep:{seconds} sleep (float) - insert delay between commands
(numerical values can be in different formats, like: 42, 0x2a, 0o52, 0b101010, 32K, 1M, ..)
Whole project is under MIT license