Serial Wire Debugging the STM32 via the Bus Pirate

2 October 2010

So 10 days ago I saw a post on Dangerous Prototypes mentioning the new STM32 Discovery board. Needless to say, I had impulse-bought a couple from Digi-Key within minutes. Unfortunately, I didn’t bother doing much research at that point, so I was unaware that the ST-Link has no Linux support until they arrived and I went to go program one. I could have just rebooted into Windows, but that seemed like admitting defeat, and I don’t really like IDEs anyway. Serendipitously, I had been reading about the Serial Wire Debug protocol for the past couple of days, and it seemed like a pretty nice little protocol, so I wondered if perhaps I could get that working.

Step 1 - The Bus Pirate

Given that I own a Bus Pirate, it seemed like the natural tool for implementing a new protocol that I had just read about. I had never used it before or even indeed verified that it worked, but I’ll spare you a long recounting of how I spent several days fighting with it, and just note that a tedious firmware upgrade process is ESSENTIAL if you bought a Bus Pirate from Sparkfun. Once that was fixed, it was a breeze setting up some basic serial communications with the Bus Pirate using the binary raw-wire mode from a Python script, although as mentioned in a previous post, the timings are slightly off compared to what one might naively expect.

Step 2 - Debug Port Communications

So given the ability to send and receive bits and bytes via the Bus Pirate, Debug Port communications were relatively straightforward. It’s important to pay attention to the fact that register numbers and data are all sent LSB-first down the wire, but otherwise it’s a nice, simple protocol.

Step 3 - Access Port Communications

So next I turned to the Cortex-M3 AHB Access Port, which would allow me to start manipulating the chip proper. I’ll again spare you several days of agonizing debugging, and just point out that when you clear the xPWRUPREQ bits, the associated features will power down. In hindsight, that makes a lot of sense. The general operation of the AHB-AP is pretty simple. Set the Control/Status Word to auto-increment if you want that feature, write the desired address into the Transfer Address Register, and manipulate the contents of the Data Read/Write register to your heart’s desire. At this point I was able to scan over the chip’s memory and make a dump of its contents, so I went ahead and made a copy of the current program in flash, just to be safe. This turned out to be very useful indeed.

Step 4 - Processor State

I knew that programming the flash would probably be tricky and possibly require the core to be halted, so I took some time at this point to do some little helper functions for state manipulation. Halting, unhalting, and restarting the processor are all pretty easy, simply involving writing some magic numbers to magic memory locations.

Step 5 - Programming the Flash

This stumped me for a while until I found the STM32 Flash Programming manual. After that it took some fiddling, but ultimately worked out pretty nicely. The process involves writing a sequence of keys to an unlock register before the control register can be written, then using the control register to erase the whole memory (in theory I could do it page-by-page, but that seemed harder and wasn’t necessary). Then the programming process consists of setting the FLASH_CR_PRG bit to indicate that programming is incoming, setting the AHB-AP to do writes in 16-bit packed mode, and writing the program data to memory starting at 0x08000000.

Step 6 - Optimizing

When I started this step, the script took 11 seconds to program a 3k firmware to the chip. When I finished, it took 1.5 seconds, and a good portion of that is required to avoid overflowing the write buffer (at least I think that’s the reason it errors if I try to decrease the interval between successive words). Those reads are very costly, so the optimizing essentially was just finding clever ways to avoid reading data from the Bus Pirate whenever possible, and only doing it in big blocks when required.

Conclusion

And that was it, modulo some issues with .bin file endianness and me having some trouble getting a decent firmware to compile properly. The code for the programming script, along with some precompiled firmwares which blink the blue LED at different rates, can be grabbed off GitHub, although there is currently no error recovery whatsoever, and only the most basic error detection.

Serial Wire Debugging the STM32 via the Bus Pirate的更多相关文章

  1. Introduction to Cortex Serial Wire Debugging

    Serial Wire Debug (SWD) provides a debug port for severely pin limited packages, often the case for ...

  2. Serial Wire Viewer (SWV)

    Being able to display values for counters, sensors and other debugging information is an important p ...

  3. Programming Internal Flash Over the Serial Wire Debug <SWD> Interface -- EFM32

    1 Debug Interface Overview 1.1 Serial Wire Debug Serial Wire Debug (SWD) is a two-wire protocol for ...

  4. Implementation of Serial Wire JTAG flash programming in ARM Cortex M3 Processors

    Implementation of Serial Wire JTAG flash programming in ARM Cortex M3 Processors The goal of the pro ...

  5. Serial Wire Debug (SWD) Interface -- PSoc5

    PSoC 5 supports programming through the serial wire debug (SWD) interface. There are two signals in ...

  6. SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic

    SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic The SW-DP protocol is described ...

  7. 各种版本的ST-LINK仿真器

    1.ST官方正式出版了两种仿真器:ST-LINK.ST-LINK/V2,其他型号(ST-LINK II,ST-LINK III,…)要么是国内公司生产,要么是开发板自带的:2.在ST官网ST-LINK ...

  8. KL46 custom board SWD reset is never asserted - SWS Waveform

    KL46 custom board SWD reset is never asserted Hi everybody, I'm trying to program a custom board bas ...

  9. windows下STM32开发环境的搭建

    一.概述 1.说明 笔者已经写了一篇Linux下STM32开发环境的搭建 ,这两篇文章的最区别在于开发环境所处的系统平台不一样,而其实这个区别对于开发环境的搭建其实影响不大,制作局部上的操作上发生了改 ...

随机推荐

  1. yii2框架目录

    框架目录结构 [目录] backend——后台web程序 common——公共的文件 console——控制台程序 environments——环境配置 frontend——前台web程序 [文件] ...

  2. js深复制

    一般来讲深复制主要是为了复制js对象中的引用类型,引用类型在普通的赋值操作下相当于是引用,修改复制对象也会影响原对象,简单的方法的话可以使用JSON.parse(JSON.stringify(obj) ...

  3. 第5月第8天 jsonmodel

    1. @implementation JSONValueTransformer (CustomTransformer) //时间戳转NSDate - (NSDate *)NSDateFromNSStr ...

  4. requests(三):json请求中中文乱码处理

    最近收到一个问题:json格式请求数据中有中文,导致服务端签名失败. 问题详情: 一位同学在发送json格式的post请求时,请求数据中有中文内容: {"inputCodes":[ ...

  5. python3之安装、pip、setuptools

    1.python3安装 下载地址:https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz #安装环境centOS 7 #安装依赖包: yum ...

  6. Install Shield中调用devcon自动安装硬件驱动程序

    1.安装驱动程序命令devcon安装好WINDDK之后,devcon.exe在"C:\WINDDK\3790.1830\tools\devcon"目录下.>devcon up ...

  7. shell函数中eof报错(warning: here-document at line 9 delimited by end-of-file (wanted `EOF'))

    在shell编写函数时,函数中有eof和EOF,如果是在sublime编写按照格式tab缩进会有以下报错 解决办法: 取消函数中的tab缩进,在运行即可

  8. 转:vue2.0 keep-alive最佳实践

    转载至:https://segmentfault.com/a/1190000008123035 1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性 ...

  9. Spring Cloud与Docker微服务架构实战 PDF

    电子版百度云下载 链接: https://pan.baidu.com/s/115u011CJ8MZzJx_NqutyTQ 提取码: 关注公众号[GitHubCN]回复2019获取 本书的代码 共计70 ...

  10. 【LOJ】 #2547. 「JSOI2018」防御网络

    题解 如果只是一棵树的话,那么就枚举每条边,分成两部分大小为\(a\)和\(b\) 那么这条边被统计的方案数是\((2^a - 1)(2^b - 1)\) 如果是一个环的话,我们枚举环上至少有\(N ...