Linux下通过Jlink烧写RT-Thread内核到stm32板子

时间:2023-02-20 16:10:34

Host:ubuntu-10.10
Rtos:RT-Thread-0.4.x
Packages: sudo aptitude install libusb
---------------------------------------------------

首先需要说明的是,稳定版RTT v0.3.1中的stm3210分支好像不支持GCC编译,所以需要升级到v0.4.x版本才能进行测试。进入v0.4.x中的bsp/stm3210目录,直接就是源文件,不像之前的版本包含那么多子目录,结构简单多了,可以直接执行“scons”进行编译,最后会有一个.bin文件生成,烧写的就是该文件(Linux用户需编辑rtconfig.py,指定CROSS_TOOL为gcc,详细参见前面的“Linux下编译RT-Thread” 一文)

然后就是设置Jlink了。Linux下有两种方法使用Jlink:

* 使用openocd
* 使用官方的基于Linux的软件包

一、使用openocd

注:在我的ubuntu10.10环境上,通过openocd方法连接Jlink失败了,不过还是将步骤记录一下,用在其它的Linux环境说不定就可以
首先安装openocd:

sudo aptitude install openocd

然后将Jlink与PC机相连;
接着执行如下命令:

$ cd /usr/share/openocd/scripts
$ openocd -f interface/jlink.cfg -f target/stm32.cfg

正常情况下应该就能连接上了,不过我这里始终有些问题,连接的log如下:

Open On-Chip Debugger 0.4.0 (2010-10-08-15:42)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.berlios.de/doc/doxygen/bugs.html
1000 kHz
jtag_nsrst_delay: 100
jtag_ntrst_delay: 100
Info : J-Link initialization started / target CPU reset initiated
Info : J-Link ARM V7 compiled Sep 6 2010 10:43:31
Info : JLink caps 0xb9ff7bbf
Info : JLink hw version 70000
Info : JLink max mem block 9000
Info : Vref = 3.300 TCK = 1 TDI = 0 TDO = 1 TMS = 0 SRST = 0 TRST = 0
 
Info : J-Link JTAG Interface ready
Info : clock speed 1000 kHz
Error: JTAG scan chain interrogation failed: all ones
Error: Check JTAG interface, timings, target power, etc.
Error: JTAG scan chain interrogation failed: all ones
Error: Check JTAG interface, timings, target power, etc.
Command handler execution failed
Warn : jtag initialization failed; try 'jtag init' again.

搜索过程中看到有人说可能是系统自带的软件包没打开对Jlink的支持云云,于是又自己手动编译了一回!
下载源码包:sourceforge.net/projects/openocd/ 并解压
然后编译安装(明确指定--enable-jlink,并安装到/opt目录下):

$ cd openocd-0.4.0
$ ./configure --prefix=/opt/openocd --enable-jlink
$ make
$ sudo make install

最后执行测试:

$ cd /opt/openocd
$ ./bin/openocd -f share/openocd/scripts/interface/jlink.cfg -f share/openocd/scripts/target/stm32.cfg

不幸的是错误依然!
看来此路暂时不通,再尝试第二种吧。。。

二、使用官方的基于Linux的软件包

首先下载“for linux”版本的软件包:www.segger.com/cms/jlink-software.html 并解压(当时版本:JLink_Linux_V422)
然后将解压目录下的库文件(libjlinkarm.so.xx)都拷贝到/usr/lib下,将文件45-jlink.rules拷贝到/etc/udev/rules.d下;
然后执行:

$ sudo /etc/init.d/udev restart

然后将Jlink连接后就可以启动了:

$ cd JLink_Linux_V422/
$ ./start

这次到蛮顺利的,没遇到什么波折;如果执行时遇到libjlinkarm.o.0库文件找不到或"file too short"之类的错误,一般都是因为库的路径不正确,参照下面的参考链接自行设置一下即可。
连接的log如下:

$ ./start 
SEGGER J-Link Commander V4.22 ('?' for help)
Compiled Dec 17 2010 17:41:09
DLL version V4.22, compiled Dec 17 2010 17:41:06
Firmware: J-Link ARM V7 compiled Sep 6 2010 10:43:31
Hardware: V7.00
S/N: 11111117
Feature(s): RDI, FlashBP, FlashDL, JFlash, GDBFull
VTarget = 3.300V
Info: Could not measure total IR len. TDO is constant high.
 
WARNING: CPU core not found.
No devices found on JTAG chain. Trying to find device on SWD.
Info: Found SWD-DP with ID 0x1BA01477
Info: TPIU fitted.
Info: ETM fitted.
Info: FPUnit: 6 code (BP) slots and 2 literal slots
Info: Found Cortex-M3 r1p1, Little endian.
Cortex-M3 identified.
JTAG speed: 100 kHz
J-Link>
J-Link>?
 
Available commands are:
----------------------
f Firmware info
h halt
g go
 
...
...
...
 
[<Interface0> <Interface1> ...]
----------------------
NOTE: Specifying a filename in command line
will start J-Link Commander in script mode.


中间省略的长长一段是Jlink支持的命令,最后两句表明可以支持脚本处理。

最后就是烧写.bin文件到stm32了,通过loadbin命令操作:

J-Link>exec device = STM32F103VE
J-Link>loadbin /home/zwang/work_dir/stm32/rt-thread-read-only/bsp/stm3210/rtt.bin 0x8000000
Loading binary file... [/home/zwang/work_dir/stm32/rt-thread-read-only/bsp/stm3210/rtt.bin]
Writing bin data into target memory @ 0x08000000.
Info: Found SWD-DP with ID 0x1BA01477
Info: TPIU fitted.
Info: ETM fitted.
Info: FPUnit: 6 code (BP) slots and 2 literal slots
Info: Found Cortex-M3 r1p1, Little endian.
J-Link>
J-Link>r
Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Info: Found SWD-DP with ID 0x1BA01477
Info: TPIU fitted.
Info: ETM fitted.
Info: FPUnit: 6 code (BP) slots and 2 literal slots
Info: Found Cortex-M3 r1p1, Little endian.
J-Link>g

关于Jlink烧写需要注意的:
1. 必须先执行“exec device = STM32F103VE”,device后面的当然是与具体使用的芯片型号相一致了;我的芯片型号是STM32F103VET6,之前执行“exec device = STM32F103VET6”是不行的!
2. loadbin是需要一个过程的,Jlink指示灯会闪烁,有时间延迟感;如果是秒速肯定就不正常了。
3. 关于烧写的地址,在链接脚本bsp/stm3210/stm32_rom.ld里面定义的有。
4. 如果是烧写大文件,也可以重新设置speed,如:

J-Link> exec device = STM32F103VE
J-Link> speed 12000
J-Link> h ---> halt
J-Link> loadbin /home/zwang/work_dir/stm32/rt-thread-read-only/bsp/stm3210/rtt.bin 0x8000000
J-Link> r ---> reset ( and make sure to really write into flash ?? )
J-Link> g

关于命令loadbin:

# loadbin    Load binary file into target memory.
# Syntax: loadbin <filename>, <addr>

有人说该命令仅能将内核加载到内存(memory),断电或重启板子之后就没了;然而实际情况并非如此,是会写入到nor flash之中的。所以这里上面的"memory"不应该仅仅理解为内存,而应该理解为存储设备,因而也就包含nor/nand flash了。

关于使用官方Linux软件包的说明:文中所提及的版本V422好像只支持Jlink V7;测试过V8是不工作的。

参考链接:
[1] blog.csdn.net/crazyleen/archive/2010/10/11/5934087.aspx
[2] www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn...gavr&bbs_id=9999
[3] eminem112.blog.163.com/blog/static/62815953201092513943796/
[4] www.segger2.com/index.php?page=Thread&threadID=385