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就可以直接運行第二步了,方便簡介.




 
      
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM