Use of BeagleBone GPIO for SWD programming

Recently I killed on of SigFox Td1208 device by misprogramming it, destroying the bootloader. In a such situation serial port is not anymore accessible for programming. The use of JTAG port is a necessity. On the TD1208, jtag has been repaced by SWD which is a 2 wires port doing the same thing.

To use this port, you generally need a specific interface. These interfaces like Jlink are expensive (> 300€), some are less like BusPirate (30€). By the way, if you do not have it, you will have to order and wait for it. That’s why I’ve taken the choice of using my BeagleBone black device GPIO to pilot these signal. This code can easily be port to RaspberryPI…

To start, for those who are curious about SWD protocol here are some good link I used to understand how it works – here & here.

Then, after trying to build my SWD stack myself and kill that way a lot of energy, I finally found this excellent project doing it in Python (easy to port on any platform) : PySWD

This project is based on BusPirate interface, my work was to rewrite an interface based on GPIO by creating BeagleBoneSWD.py file. The file is accessible from my fork of the project. The link to this fork is here.

Then, to use it, you should start reading the BeagleBone SWD header to get information on pinout, then make some little change in the file you want to use this new interface.

Here is an example to Flash a EFM32 chip:

File : flashFM32.py

import time
import sys
import array

from PirateSWD import *
from SWDCommon import *
from EFM32 import *

def loadFile(path):
    arr = array.array('I')
    try:
        arr.fromfile(open(sys.argv[1], 'rb'), 1024*1024)
    except EOFError:
        pass
    return arr.tolist()

def main():
    busPirate = PirateSWD("/dev/tty.usbserial-buspirat", vreg = True)

The modification to be made are in red :

File : flashFM32.py

import time
import sys
import array

from PirateSWD import *
from BeagleBoneSWD import *
from SWDCommon import *
from EFM32 import *

def loadFile(path):
    arr = array.array('I')
    try:
        arr.fromfile(open(sys.argv[1], 'rb'), 1024*1024)
    except EOFError:
        pass
    return arr.tolist()

def main():
    busPirate = BeagleBoneSWD("", vreg = True)   

Now the SWD interface can be accessed using beagle-bone gpio.

The BeagleBoneSWD.py File : BeagleBoneSWD.py

The execution to flash a device is really long as the use of file system gpio is really long, it has taken about 1 hour and an half to flash my device. In my case it was not a problem as it is the way to rebirth this device, then I’m able to flash it over serial port. By-the-way, the next time, I’ll optimize it by looking at the native python gpio libraries …

To connect the TD1204, TD1208, TD1205, you need to use a such schematic

Connect an EFM32 / TD chip to BeagleBone with SWD

Connect an EFM32 / TD chip to BeagleBone with SWD

The second way to pilot the GPIO is to use Adafruit_BBIO lib ; to install this lib get the tgz and  execute on BBB

#set the date and time
/usr/bin/ntpdate -b -s -u pool.ntp.org
#install dependency
opkg update && opkg install python-distutils
cd adafruit-beaglebone-io-python
python setup.py install

Then you can use BeagleBoneNewSWD.tar instead of the previous one. Do not forget to adapt the flashEFM32 file also.

Note that this code is better to be read, but it is not really faster 🙁

7 thoughts on “Use of BeagleBone GPIO for SWD programming

  1. Hello! I love this post. I tried running this on Raspberry Pi, connected to SWD pins of a STM32VL Discovery board, but it doesn’t really seem to work that way. Is there anything that should be changed, besides the pins? The program seems to break, getting the SWD Fault error.

    Thank you for the post anyway!

    • The code has to be modified for RPI, I have in mind to do it, but not yet found time for this… If you do it, I’ll be happy to share it ! basically you just have a couple of line to modify to change the clk and data signals

      • That’s what I’ve been trying to do actually. I changed the GPIO numbers for RPI suitable ones. I have my STM32VL connected on those pins, however I haven’t been able to succesfully program it. The program gets SWD fault error in different parts of flashing, sometimes it goes real far, like up to the programming flash section, but seems to break nevertheless. Did something like this happen on BBB? Is there anything else that should be changed? I would be happy if you could help.

      • did you add the line termination resistors ? Eventually, add a small sleep in the sleep function.

      • The line is fine and it seems to be working until I get a FAULT ACK from stm32.
        Is there any chance you could try doing this on RPi? I would be extremely grateful. I tried to play with the delays and it didn’t seem to help.

      • sorry, I won’t have time in this period, next time I will have to reflash I’ll think to do it. How many time it works before the Fault ACK ? can you eventually dump me the console trace ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.