STM32F103

如何使用N900透過ST-LINK V2燒錄程式(OpenOCD)


ST-LINK V2


開發板


連接ST-LINK V2到N900和STM32F103開發板


正面圖


ST-LINK V2和STM32F103開發板的接線關係

ST-LINK V2STM32F103
3.3V3.3V
GNDGND
SWDIOPA13
SWCLKPA14

切換到USB Host Mode


使用dmesg看一下USB訊息

$ dmesg
  [32247.539093] hub 1-0:1.0: debounce: port 1: total 100ms stable 100ms status 0x101
  [32247.664093] usb 1-1: new full speed USB device using musb_hdrc and address 4
  [32247.813781] usb 1-1: default language 0x0409
  [32247.814971] usb 1-1: uevent
  [32247.815093] usb 1-1: usb_probe_device
  [32247.815124] usb 1-1: configuration #1 chosen from 1 choice
  [32247.815887] usb 1-1: adding 1-1:1.0 (config #1, interface 0)
  [32247.816040] usb 1-1:1.0: uevent
  [32247.817413] drivers/usb/core/inode.c: creating file '004'
  [32247.817779] usb 1-1: New USB device found, idVendor=0483, idProduct=3748
  [32247.817810] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
  [32247.817810] usb 1-1: Product: STM32 STLink
  [32247.817840] usb 1-1: Manufacturer: STMicroelectronics
  [32247.817840] usb 1-1: SerialNumber: WÿtH??VSV5??
  [32247.817871] hub 1-0:1.0: state 7 ports 1 chg 0000 evt 0002
  [32247.817932] hub 1-0:1.0: port 1 enable change, status 00000103

因為需透過openocd燒錄程式,而舊版openocd並沒有ST-LINK V2的配置檔,因此,司徒建議自己手動編譯openocd(目前是0.10.0),步驟如下:

$ cd
$ git clone git://repo.or.cz/openocd.git
$ cd openocd
$ ./bootstrap
$ ./configure
$ make
$ sudo make install

接著看一下目前系統上的USB裝置


接著寫一個LED閃爍的程式(led.S)

  .thumb
  .syntax unified
  .equ GPIOC_CRH,   0x40011004
  .equ GPIOC_ODR,   0x4001100c
  .equ RCC_APB2ENR, 0x40021018
  .equ STACKINIT,   0x20005000
  .equ LEDDELAY,    800000

  .global _start
  .section .text
  .org 0x0
  .word STACKINIT
  .word _start

  .org 0x100
  .thumb_func
_start:
  ldr r6, =RCC_APB2ENR
  mov r0, 0x10
  str r0, [r6]
  ldr r6, =GPIOC_CRH
  ldr r0, =0x44344444
  str r0, [r6]
  mov r2, 0
  mov r3, 0x2000
  ldr r6, =GPIOC_ODR
loop:
  str r2, [r6]
  ldr r1, =LEDDELAY
delay1:
  subs r1, 1
  bne delay1
  str r3, [r6]
  ldr r1, =LEDDELAY
delay2:
  subs r1, 1
  bne delay2
  b loop

LD Script內容(led.ld)

SECTIONS
{
  . = 0x0;
  .text : 
  {
    *(.text)
  }
  .data : 
  {
    *(.data)
    *(.rom)
  }
  . = 0x20000000;
  .ram : { *(.ram) }

  .bss : 
  {
    *(.bss)
    *(.ram)
  }
}

Makefile

all:
  as -mcpu=cortex-m3 -mthumb -mthumb-interwork -o led.o led.S
  ld -T led.ld -o led.elf led.o
  objcopy -O binary led.elf led.bin

clean:
  rm -rf led.o led.elf led.bin

編譯程式


使用openocd連接ST-LINK V2燒錄器並指定開發板型號

$ sudo openocd -f /usr/local/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/local/share/openocd/scripts/target/stm32f1x.cfg



連接到openocd燒錄介面並且燒錄led.bin程式

$ telnet localhost 4444
  > halt
  > flash erase_sector 0 0 0
  > flash write_bank 0 led.bin 0
  > reset init
  > resume 




完成



如果使用者不想透過telnet操作,可以將Makefile改成如下:

all:
  as -mcpu=cortex-m3 -mthumb -mthumb-interwork -o led.o led.S
  ld -T led.ld -o led.elf led.o
  objcopy -O binary led.elf led.bin
  sudo openocd -f /usr/local/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/local/share/openocd/scripts/target/stm32f1x.cfg -c "program led.bin reset exit 0x8000000"

clean:
  rm -rf led.o led.elf led.bin

P.S. 透過修改Makefile,使用者現在只需執行make指令即可完成編譯和燒入的動作,當然,如果編譯有問題時,make會停止其餘動作。


返回上一頁