Skip to main content

svd_gdb

Talk to microcontrollers through a debug probe.

Example 1

We have an nRF51822 on a Black Magic Probe. Let’s see its unique device ID:

>>> from svd_gdb.drivers.nrf5x import NRF51
>>> d = NRF51()
>>> d.FICR.DEVICEID
[nrf51.FICR.DEVICEID[0] = 0xb5b527cd, nrf51.FICR.DEVICEID[1] = 0x15896259]

The nRF51 has a random number generator. Let’s collect some random numbers with it:

>>> d.RNG.TASKS_START=1
>>> bytes(d.RNG.VALUE for _ in range(15))
b'\xa8qS\x0e\x86\xacw\xfb\x7fP\xb2\x02a9X'

Let’s see why the last reset happened:

>>> d.POWER.RESETREAS._dump()
            nrf51.POWER.RESETREAS
            | Reset reason.
0x40000400: 0x   0   0   0   0   0   0   0   4
            0b00000000000000000000000000000100
                           ^^^            ^^^^
                           |||            |||+ RESETPIN = 0x0 (NotDetected) | Reset from pin-reset detected.
                           |||            ||+- DOG = 0x0 (NotDetected) | Reset from watchdog detected.
                           |||            |+-- SREQ = 0x1 (Detected) | Reset from AIRCR.SYSRESETREQ detected.
                           |||            +--- LOCKUP = 0x0 (NotDetected) | Reset from CPU lock-up detected.
                           ||+---------------- OFF = 0x0 (NotDetected) | Reset from wake-up from OFF mode detected by the use of DETECT signal from GPIO.
                           |+----------------- LPCOMP = 0x0 (NotDetected) | Reset from wake-up from OFF mode detected by the use of ANADETECT signal from LPCOMP.
                           +------------------ DIF = 0x0 (NotDetected) | Reset from wake-up from OFF mode detected by entering into debug interface mode.

Example 2

We have a Bluepill board on the probe. A Bluepill has an STM32F103 processor, with an LED on pin C13. Let’s toggle it on:

>>> from svd_gdb.drivers.stm32 import STM32F103
>>> d = STM32F103()
>>> d.PC_13.o ^= 1

Let’s see if pin C12 is floating:

>>> d.PC_12.hiz
False

It is not. Is it pulled high or low?

>>> d.PC_12.i
0

That means it is pulled low.

Rationale

What is a Python library like this useful for?

  • Production test scripting.

    In a production test of a microcontroller system, you are already connected to a debugger, to program the application code.

    Using this library, in addition to programming code, you can do basic tests on connectivity. If your script can measure supply current, you can verify that LEDs are installed correctly. You can take readings from sensors on the board under test, and store calibration parameters.

    For comparison to alternative approaches to production testing, see Appendix A.

  • Development

    For a typically poorly-specified sensor like a commercial accelerometer, there is a fair amount of trying-and-tuning different register settings to find an optimal configuration. Using this library (perhaps with a wireless debugger), the settings can be determined in the actual circuit, before writing any firmware code.

    The same is even true for microcontroller peripherals – sometimes it is easier to prototype their operation in a scripting environment.

    I have used the library to prototype LED blinking behavior in actual hardware, for the approval of product management. We iterated through a handful of different blinking patterns in a few minutes, much faster than an approach that involves compilation cycles, or an approach that requires designing and implementing a “blink specification protocol” to control firmware at run time.

Implementation

This script uses gdb.py from the Black Magic Probe project, and extends it with register definitions from the SVD (“system view description”) file provided by the microcontroller’s vendor.

It is no accident that the SVD file happens to contain exactly the correct information to make an ergonomic Python interface to the microcontroller functionality. This kind of thing is what SVD was invented to do.

This package gets the SVD files on demand from the cmsis-svd project. But it uses its own parser.

Other projects

To use SVD files for more convenient debugging within the gdb’s shell, see https://github.com/bnahill/PyCortexMDebug

Metadata

Release files for svd-gdb 0.0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for svd-gdb 0.0.13
File Size Uploaded Provenance Actions
svd_gdb-0.0.13.tar.gz 104.2 kB Without provenance Details for svd_gdb-0.0.13.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for svd-gdb 0.0.13
File Interpreter ABI Platform Actions
svd_gdb-0.0.13-py3-none-any.whl Python 3 none any Details for svd_gdb-0.0.13-py3-none-any.whl

Total release size: 207.9 kB

Release files / svd_gdb-0.0.13.tar.gz

Download URL svd_gdb-0.0.13.tar.gz
Size 104.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bdae60c8a486b2ad00f22178ce6115f1a732028af30d2b4622c4dc309927c741
BLAKE2b-256 checksum
How to use checksums
82ebce418af8ed73838efa06c7eecf1246b622d4b767ba2dec8e4e8766899e83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release files / svd_gdb-0.0.13-py3-none-any.whl

Download URL svd_gdb-0.0.13-py3-none-any.whl
Size 103.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de9756cd21457487c36123e674e171270372ebdd363adc1b0c362bcbf7367a6d
BLAKE2b-256 checksum
How to use checksums
51a91e1e66b4de6a07fe6098e02d196cfc01383b5823e38cff412ed689153e73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Security for svd-gdb 0.0.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page