ubuntu linux下建立stm32开发环境: 程序烧录 openocd+openjtag
原文出处: http://blog.csdn.net/embbnux/article/details/17619621
之前建立stm32开发环境,程序也已经编译好生成main.bin,接下来就是要把该文件烧录到stm32上.在linux下给arm烧录程序主要使用openocd,这个软件开源,而且支持众多芯片,从ARM9到A8都可以,当然STM32也可以.支持的JTAG工具也很多,JLINK ST-LINK OSBDM都可以,我这正好有一个openjtag基于FT2232C的,也是被支持的.
个人原创,转载请注明
参考:
How-to manual Installing a toolchain for Cortex-M3/STM32 on Ubuntu by Peter Seng
一 安装openocd
在ubuntu下安装openocd
sudo apt-get install openocd
也可以到官网下载源码包自己编译
二 安装openjtag驱动
插上openjtag
user@ubuntu:~/$ lsusb
Bus 002 Device 005: ID 093a:2521 Pixart Imaging, Inc.
Bus 002 Device 003: ID 1457:5118 First International Computer, Inc. OpenMoko Neo1973 Debug board (V2+)
第二个就是了,记下ID 1457:5118
sudo gedit /etc/udev/rules.d/45-ftdi2232-libftdi.rules 在里面添加
SYSFS{idProduct}=="5118", SYSFS{idVendor}=="1457", MODE="666", GROUP="plugdev"
权限666,使用openocd就不用sudo了.
sudo /etc/init.d/udev restart
拔下在插上就可以了.
三 使用openocd 连openjtag
JTAG接口配置文件openjtag.cfg.根据JTAG设备不同,修改下面
#interface configuration openjtag#############################
interface ft2232
ft2232_device_desc "USB<=>JTAG&RS232"
ft2232_layout jtagkey
ft2232_vid_pid 0x1457 0x5118
可以参考openocd目录下的文件:/usr/share/openocd/scripts/interface,主要是设备ID以及设备名字,可以通过dmesg | grep usb命令查看.
要烧录stm32f103就得有这个设备的相关配置,可以查看/usr/share/openocd/scripts/target/stm32f1x.cfg
这里把两个文件合在一起openocd.cfg
#daemon configuration###############################################################
telnet_port 4444
gdb_port 3333
#interface configuration openjtag#############################
interface ft2232
ft2232_device_desc "USB<=>JTAG&RS232"
ft2232_layout jtagkey
ft2232_vid_pid 0x1457 0x5118
#board configuration################################################################
# Adjust Work-area size (RAM size) according to MCU in use:
#STM32F103RB --> 20KB
#set WORKAREASIZE 0x5000
#STM32F103ZE --> 64KB
set WORKAREASIZE 0x10000
#target configuration###############################################################
# script for stm32f1x family
if { [info exists CHIPNAME] } {
set _CHIPNAME $CHIPNAME
} else {
set _CHIPNAME stm32f1x
}
if { [info exists ENDIAN] } {
set _ENDIAN $ENDIAN
} else {
set _ENDIAN little
}
# Work-area is a space in RAM used for flash programming
# By default use 16kB
if { [info exists WORKAREASIZE] } {
set _WORKAREASIZE $WORKAREASIZE
} else {
set _WORKAREASIZE 0x4000
}
# JTAG speed should be <= F_CPU/6. F_CPU after reset is 8MHz, so use F_JTAG = 1MHz
adapter_khz 500
adapter_nsrst_delay 100
jtag_ntrst_delay 100
#jtag scan chain
if { [info exists CPUTAPID] } {
set _CPUTAPID $CPUTAPID
} else {
# See STM Document RM0008
# Section 31.6.3
set _CPUTAPID 0x3ba00477
}
jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID
if { [info exists BSTAPID] } {
# FIXME this never gets used to override defaults...
set _BSTAPID $BSTAPID
} else {
# See STM Document RM0008
# Section 31.6.2
# Low density devices, Rev A
set _BSTAPID1 0x06412041
# Medium density devices, Rev A
set _BSTAPID2 0x06410041
# Medium density devices, Rev B and Rev Z
set _BSTAPID3 0x16410041
set _BSTAPID4 0x06420041
# High density devices, Rev A
set _BSTAPID5 0x06414041
# Connectivity line devices, Rev A and Rev Z
set _BSTAPID6 0x06418041
# XL line devices, Rev A
set _BSTAPID7 0x06430041
# VL line devices, Rev A and Z In medium-density and high-density value line devices
set _BSTAPID8 0x06420041
# VL line devices, Rev A
set _BSTAPID9 0x06428041
}
jtag newtap $_CHIPNAME bs -irlen 5 -expected-id $_BSTAPID1 \
-expected-id $_BSTAPID2 -expected-id $_BSTAPID3 \
-expected-id $_BSTAPID4 -expected-id $_BSTAPID5 \
-expected-id $_BSTAPID6 -expected-id $_BSTAPID7 \
-expected-id $_BSTAPID8 -expected-id $_BSTAPID9
set _TARGETNAME $_CHIPNAME.cpu
target create $_TARGETNAME cortex_m -endian $_ENDIAN -chain-position $_TARGETNAME
$_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
# flash size will be probed
set _FLASHNAME $_CHIPNAME.flash
flash bank $_FLASHNAME stm32f1x 0x08000000 0 0 0 $_TARGETNAME
# if srst is not fitted use SYSRESETREQ to
# perform a soft reset
cortex_m reset_config sysresetreq
开始烧录:
< 1 > 在一个终端下执行:
openocd -f openocd.cfg
出现:
Open On-Chip Debugger 0.7.0 (2013-05-15-17:28)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.sourceforge.net/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'jtag'
adapter speed: 500 kHz
adapter_nsrst_delay: 100
jtag_ntrst_delay: 100
cortex_m3 reset_config sysresetreq
Info : clock speed 500 kHz
Info : JTAG tap: stm32f1x.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32f1x.bs tap/device found: 0x06414041 (mfg: 0x020, part: 0x6414, ver: 0x0)
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
没有提示出错,就表示连接上STM32了 .如果出现出错,就在开发板上按下RESET 键复位,查看BOOT0和BOOT1有没有设置出错.
< 2 > 在另一个终端下,输入:
telnet localhost 4444
依次输入:
reset halt
flash probe 0
stm32f1x mass_erase 0
flash write_bank 0 /you_stm32_project_dir/main.bin 0
reset run
程序就烧好了,按下reset键,就开始运行了.
要输入这么多命令太麻烦了,写个perl脚本使它一步运行.
首先安装perl-telnet
sudo apt-get install libnet-telnet-perl
在工程目录下新建do_flash.pl文件
#!/usr/bin/perl
use Net::Telnet;
$numArgs = $#ARGV + 1;
if($numArgs != 1){
die( "Usage ./do_flash.pl [main.bin] \n");
}
$file = $ARGV[0];
$ip = "127.0.0.1";
$port = 4444;
$telnet = new Net::Telnet (
Port => $port,
Timeout=>10,
Errmode=>'die',
Prompt =>'/>/');
$telnet->open($ip);
print $telnet->cmd('reset halt');
print $telnet->cmd('flash probe 0');
print $telnet->cmd('stm32f1x mass_erase 0');
print $telnet->cmd('flash write_bank 0 '.$file.' 0');
print $telnet->cmd('reset halt');
print $telnet->cmd('exit');
print "\n";
在根目录下的Makefile文件里面加入这段语句:
flash:all
./do_flash.pl $(TOP)/main.bin
这样只要,执行make flash就可以直接运行第二步了,方便简介.
若在页首无特别声明,本篇文章由 Schips 经过整理后发布。
博客地址:https://www.cnblogs.com/schips/