首页 > 技术文章 > ubuntu下配置openocd+FT2232

jacob1934 2014-11-04 23:30 原文

本文原地址  http://www.cnblogs.com/jacob1934/p/4075184.html
请勿转载
【1】确认调试器方案
将调试器插到电脑上,lsusb看一下:

Bus 003 Device 009: ID 1457:5118 First International Computer, Inc. OpenMoko Neo1973 Debug board (V2+)

dmesg看一下:

[ 1223.230752] usb 3-12: new full-speed USB device number 7 using xhci_hcd
[ 1223.364173] usb 3-12: New USB device found, idVendor=1457, idProduct=5118
[ 1223.364181] usb 3-12: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 1223.364185] usb 3-12: Product: USB<=>JTAG&RS232
[ 1223.364188] usb 3-12: Manufacturer: www.100ask.net
[ 1223.366931] usb 3-12: Ignoring serial port reserved for JTAG
[ 1223.369248] ftdi_sio 3-12:1.1: FTDI USB Serial Device converter detected
[ 1223.369341] usb 3-12: Detected FT2232C
[ 1223.369349] usb 3-12: Number of endpoints 2
[ 1223.369356] usb 3-12: Endpoint 1 MaxPacketSize 64
[ 1223.369362] usb 3-12: Endpoint 2 MaxPacketSize 64
[ 1223.369369] usb 3-12: Setting MaxPacketSize 64
[ 1223.369636] usb 3-12: FTDI USB Serial Device converter now attached to ttyUSB0

OK,确认为FT2232C方案(实际上是FT2232D,两者类似),后面要根据这个配置OPENOCD。

【2】增加udev规则,以便使用普通用户权限操作我们的调试器。

插上openocd,dmesg看一下:

以root权限创建/etc/udev/rules.d/45-openjtag.rules(文件名可以换成别的),内容如下:

SYSFS{idProduct}=="5118", SYSFS{idVendor}=="1457", MODE="666", GROUP="plugdev"

添加规则文件后,重新插拔调试器生效。

【3】确认JTAG线序,并连接设备与调试器。以下是100ask.com的OPENJTAG线序。可能需要使用转接板或杜邦线连接设备和调试器,EB-SAM9G45需要使用自带的20pin-10pin转接板。

信号名
方向
说 明
Vref
cpu->jtag
接口电平参考电压。20pin中为1脚。ft2232不支持接口电平检测,实际中用3.3V接口电压的较多
Vsupply
cpu->jtag
电源。20pin中为2脚
nTRST
jtag->cpu
(可选项) TAP reset,低电平有效,用来复位目标板上的tap控制器,可以通过tms完成。在目标端应加适当的上拉电阻以防止误触发。
TDI
jtag->cpu
测试数据输入
TMS
jtag->cpu
测试模式选择
TCK
jtag->cpu
测试时钟
RTCK
cpu->jtag
(可选项)反馈测试时钟。由目标端反馈给jtag板的时钟信号,实现TCK的自适应。不使用时可以直接接地。
TDO
cpu->jtag
测试数据输出
nSRST
Input/Output
(可选项) system reset,与目标板上的系统复位信号相连。可以直接对目标系统复位,同时可以检测目标系统的复位情况。为了防止误触发,应在目标端加上适当的上拉电阻。

JTAG引脚说明

【4】安装openocd。

【4.1】ubuntu 14.10的版本库中,openocd的当前版本是0.8。ubuntu 14.04中是0.7。

apt-get install openocd

【4.2】下载源码包安装

sourceforge页面:http://sourceforge.net/projects/openocd/
openocd-0.10.0正式版已经于2017.1.22发布
我下载的文件为openocd-0.8.0.tar.bz2,解压出来的文件为:

openocd-0.8.0:
aclocal.m4    config.sub        Doxyfile.in  missing     NEWTAPS
AUTHORS       config_subdir.m4  HACKING      NEWS        README
BUGS          configure         INSTALL      NEWS-0.2.0  README.OSX
ChangeLog     configure.ac      install-sh   NEWS-0.3.0  README.Windows
common.mk     contrib           jimtcl       NEWS-0.4.0  src
compile       COPYING           ltmain.sh    NEWS-0.5.0  tcl
config.guess  depcomp           Makefile.am  NEWS-0.6.0  TODO
config.h.in   doc               Makefile.in  NEWS-0.7.0  tools

安装依赖库:

apt-get install libusb-1.0-0-dev libftdi-dev

编译安装,这里安装到了/opt/openocd

./configure --enable-ftdi --prefix=/opt/openocd
make
sudo mkdir /opt/openocd
sudo chmod a+w opt/openocd
make install

【4.3】通过git获取最新开发版:

git clone git://git.code.sf.net/p/openocd/code openocd-code

得到的文件为:

openocd-code:
AUTHORS            configure.ac  jimtcl       NEWS-0.6.0      src
AUTHORS.ChangeLog  contrib       Makefile.am  NEWS-0.7.0      tcl
bootstrap          COPYING       NEWS         NEWS-0.8.0      testing
BUGS               doc           NEWS-0.2.0   NEWTAPS         TODO
ChangeLog          Doxyfile.in   NEWS-0.3.0   README          tools
common.mk          guess-rev.sh  NEWS-0.4.0   README.OSX      uncrustify.cfg
config_subdir.m4   HACKING       NEWS-0.5.0   README.Windows

下载子模块,编译安装:

./bootstrap
./configure --enable-ftdi --prefix=/opt/openocd
make
sudo mkdir /opt/openocd
sudo chmod a+w opt/openocd
make install

【5】配置openocd
【5.1】接口部分,我这里的配置文件路径为 ~/.openocd/openjtag.cfg,内容如下:

#openocd 0.8中默认使用新的ftdi接口,如果希望使用旧的ft2232接口,需要在编译openocd时指定。
interface ftdi
#通过描述来指定设备,应当和dmesg中的Product一致
#ftdi_device_desc "USB<=>JTAG&RS232"
#通过PID VID指定调试器,比描述更准确些
ftdi_vid_pid 0x1457 0x5118
#如果有多个同样的调试器,还可以指定要使用的调试器序列号
#ftdi_serial

#引脚定义,相当于旧版中的 ft2232_layout jtagkey
ftdi_layout_init 0x0c08 0x0f1b
ftdi_layout_signal nTRST -data 0x0100 -noe 0x0400
ftdi_layout_signal nSRST -data 0x0200 -noe 0x0800

此时可以尝试运行一下openocd,以检查连接和配置情况:

1.关闭设备电源,
2.链接设备、调试器、PC
3.打开设备电源(因为没有指定处理器,只能先打开设备以进行自动探测)
4.运行openocd:

openocd -f ~/.openocd/openjtag.cfg -c jtag_rclk 3000

该命令中,  -f 指定配置文件,可以如 -f a.cfg -f b.cfg指定多个配置文件;
       -c 执行一条命令,这里是处理器不支持RCLK时,调试器使用的时钟。
openocd的打印如下:

Open On-Chip Debugger 0.8.0 (2014-05-10-23:20)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.sourceforge.net/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'jtag'
RCLK - adaptive
RCLK - adaptive
Info : RCLK (adaptive clock speed) not supported - fallback to 3000 kHz
Warn : There are no enabled taps.  AUTO PROBING MIGHT NOT WORK!!
Warn : AUTO auto0.tap - use "jtag newtap auto0 tap -expected-id 0x0792603f ..."
Warn : AUTO auto0.tap - use "... -irlen 4"
Warn : gdb services need one or more targets defined

这里自动探测出一个TAP(Test Access Ports,设备上的JTAG核心,一个芯片中可能有多个TAP)出来,expected-id与预计(如果openocd支持该处理器,可以翻阅配置文件,否则请翻看datasheet等)的相同,这证明JTAG接口是连接和配置正确的。
如果是类似这样的打印,请检查JTAG线序、openocd配置的接口定义、目标板电源等:

Error: JTAG scan chain interrogation failed: all zeroes
Error: Check JTAG interface, timings, target power, etc.
Error: Trying to use configured scan chain anyway...
Error: IR capture error at bit 0, saw 0x00 not 0x...3
Warn : Bypassing JTAG setup events due to errors
Warn : gdb services need one or more targets defined

  我之前遇到的情况是线序和ftdi_layout都不对……

【5.2】处理器部分。
openocd 0.8已经支持sam9g45,配置文件为 target/at91sam9g45.cfg,该文件引用了同目录下的at91sam9.cfg。整理后内容如下:

#尝试使用设备提供的调试时钟(RTCK引脚),失败则使用指定的频率(单位KHZ)
adapter_khz 3

#复位配置 reset_config trst_and_srst separate trst_push_pull srst_open_drain adapter_nsrst_delay
300 jtag_ntrst_delay 200 #给即将添加的TAP配置起个名字 set _CHIPNAME at91sam9g45 #芯片TAP的识别ID,整个AT91SAM9系列都是用这个TAP ID set _CPUTAPID 0x0792603f #小端存储 set _ENDIAN little #以$_CHIPNAME为名,创建一个新的tap jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID #目标处理器定义 set _TARGETNAME $_CHIPNAME.cpu target create $_TARGETNAME arm926ejs -endian $_ENDIAN -chain-position $_TARGETNAME -variant arm926ejs # 处理器内建SRAM $_TARGETNAME configure -work-area-phys 0x00300000 -work-area-size 0x200000 -work-area-backup 1

尝试运行openocd:
1.关闭设备电源,
2.链接设备、调试器、PC
3.打开设备电源
4.运行openocd:

openocd -f ~/.openocd/openjtag.cfg -f target/at91sam9g45.cfg
target/at91sam9g45.cfg实际上在/usr/share/openocd/scripts/中,这里使用默认的搜索路径。

openocd的打印如下:

Info : only one transport option; autoselect 'jtag'
RCLK - adaptive
trst_and_srst separate srst_gates_jtag trst_push_pull srst_open_drain connect_deassert_srst
adapter_nsrst_delay: 300
jtag_ntrst_delay: 200
adapter speed: 3 kHz
1
Info : clock speed 3 kHz
Info : JTAG tap: at91sam9g45.cpu tap/device found: 0x0792603f (mfg: 0x01f, part: 0x7926, ver: 0x0)
Info : Embedded ICE version 6
Info : at91sam9g45.cpu: hardware has 2 breakpoint/watchpoint units

获得了ICE版本号,没有报错。

5.保持openocd运行,给设备重新上电。openocd在设备上电后,打印如下:

Error: invalid mode value encountered 0
target state: halted
target halted in Thumb state due to debug-request, current mode: Thread
cpsr: 0x00000020 pc: 0xffffffee
MMU: disabled, D-Cache: disabled, I-Cache: disabled

如果在设备断电的情况下启动openocd,打印的结果如5.1中所述。此时给设备上电,openocd也会打印上面的信息,同样可以证明工作正常(有调试器的情况下上电,停在最初的状态下)。

【5.3】板子部分。这部分可以定义板载的外设(如NAND)、设定一些事件发生(如CPU复位)后执行的动作等。
nand的设置:

set _NANDNAME $_TARGETNAME.nand
nand device $_NANDNAME at91sam9 $_TARGETNAME 0x40000000 0xFFFFE800

【5.4】telnet监听端口设置。

#telnet的监听端口,默认4444.可以通过telnet执行jtag指令,openocd文档中有jtag指令的解释。
#telnet_port 4444

【5.5】gdb监听端口设置。

#gdbsever的TCP/IP监听端口,默认3333.设定为disable,关闭gdbserver;pipe为标准输入输出,也就是管道
#gdb_port 3333

 【6】nand操作
首先打开一个终端,链接openocd的telnet

telnet 127.0.0.1 4444

在telnet中,将设备复位并立刻停止运行,探查nand。reset;halt需要写在一起,以保证mmu是关闭的,否则nand probe可能会返回 Address translation failure

reset;halt
nand probe 0

读写nand

nand dump 0 ~/uboot_old.bin 0 0x40000
nand erase 0 0 0x40000
nand write 0 ~/uboot_new.bin 0

 



 

 

 

 

【】SWD
transport select swd 需要写在 interface ftdi 后,否则会提示

Error: Debug adapter doesn't support any transports?

0.8版本中会做如下提示,也许只能支持特定的interface。

Info : only one transport option; autoselect 'jtag'
Error: session's transport is already selected.

0.9-dev版本中,会提示:

Info : FTDI SWD mode enabled
swd
Error: SWD mode is active but SWD_EN signal is not defined
in procedure 'init'

接下来需要设置引脚上的信号,以后再研究。

本文原地址  http://www.cnblogs.com/jacob1934/p/4075184.html请勿转载

推荐阅读