xubuntu openocd nRF51822 download

最近发愁怎么在xubuntu系统里下载nRF51822和STM32F405RGT6的事情,今天终于下载成功,记录下解决的过程。

进入到bitcraze共享的Virtual Box虚拟机之后,进入到如下目录:

cd /home/bitcraze/projects/crazyflie2-nrf-firmware/

我们会发现Makefile文件,

#Put your personal build config in config.mk and DO NOT COMMIT IT!
-include config.mk

CLOAD_SCRIPT ?= ../crazyflie-clients-python/bin/cfloader

S110 ?= 1     # SoftDevice flashed or not
BLE  ?= 1     # BLE mode activated or not. If disabled, CRTP mode is active

CROSS_COMPILE?=arm-none-eabi-

CC=$(CROSS_COMPILE)gcc
AS=$(CROSS_COMPILE)as
LD=$(CROSS_COMPILE)gcc
OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size
GDB=$(CROSS_COMPILE)gdb

OPENOCD           ?= openocd
OPENOCD_DIR       ?= 
OPENOCD_INTERFACE ?= $(OPENOCD_DIR)interface/stlink-v2.cfg
OPENOCD_TARGET    ?= target/nrf51_stlink.tcl

NRF51_SDK ?= nrf51_sdk/nrf51822
NRF_S110 ?= s110

INCLUDES= -I Include -I Include/gcc -Iinterface

PERSONAL_DEFINES ?= 

PROCESSOR = -mcpu=cortex-m0 -mthumb
NRF= -DNRF51
PROGRAM=cf2_nrf

CFLAGS=$(PROCESSOR) $(NRF) $(PERSONAL_DEFINES) $(INCLUDES) -g3 -O0 -Wall# -fdata-sections
CFLAGS+= -fsingle-precision-constant -ffast-math
# --specs=nano.specs -flto
ASFLAGS=$(PROCESSOR)
LDFLAGS=$(PROCESSOR) -O0 --specs=nano.specs -Wl,-Map=$(PROGRAM).map# -Wl,--gc-sections
ifdef SEMIHOSTING
LDFLAGS+= --specs=rdimon.specs -lc -lrdimon
CFLAGS+= -DSEMIHOSTING
endif

ifeq ($(strip $(S110)), 1)
LDFLAGS += -T gcc_nrf51_s110_xxaa.ld
CFLAGS += -DS110=1
else
LDFLAGS += -T gcc_nrf51_blank_xxaa.ld
endif

ifeq ($(strip $(BLE)), 1)
CFLAGS += -DBLE=1

OBJS += src/ble/ble.o
OBJS += src/ble/ble_crazyflies.o
OBJS += src/ble/timeslot.o

OBJS += $(NRF51_SDK)/Source/ble/ble_advdata.o
OBJS += $(NRF51_SDK)/Source/ble/ble_conn_params.o
OBJS += $(NRF51_SDK)/Source/ble/ble_services/ble_srv_common.o
OBJS += $(NRF51_SDK)/Source/ble/ble_services/ble_dis.o
OBJS += $(NRF51_SDK)/Source/sd_common/softdevice_handler.o
OBJS += $(NRF51_SDK)/Source/app_common/app_timer.o


CFLAGS += -DBLE_STACK_SUPPORT_REQD -DNRF51 
CFLAGS += -I$(NRF51_SDK)/Include/gcc 
CFLAGS += -I$(NRF51_SDK)/Include/ 
CFLAGS += -I$(NRF51_SDK)/Include/ble/ 
CFLAGS += -I$(NRF51_SDK)/Include/ble/ble_services/ 
CFLAGS += -I$(NRF_S110)/s110_nrf51822_7.0.0_API/include
CFLAGS += -I$(NRF51_SDK)/Include/app_common/ 
CFLAGS += -I$(NRF51_SDK)/Include/sd_common/ 
endif


OBJS += src/main.o gcc_startup_nrf51.o system_nrf51.o src/uart.o \
        src/syslink.o src/pm.o src/systick.o src/button.o src/swd.o src/ow.o \
        src/ow/owlnk.o src/ow/ownet.o src/ow/owtran.o \
        src/ow/crcutil.o src/ds2431.o src/ds28e05.o src/esb.o src/memory.o


all: $(PROGRAM).elf $(PROGRAM).bin $(PROGRAM).hex
	$(SIZE) $(PROGRAM).elf
ifeq ($(strip $(S110)),1)
	@echo "S110 Activated"
else
	@echo "S110 Disabled"
endif
ifeq ($(strip $(BLE)),1)
	@echo "BLE  Activated"
else
	@echo "BLE  Disabled"
endif


$(PROGRAM).hex: $(PROGRAM).elf
	$(OBJCOPY) $^ -O ihex $@


$(PROGRAM).bin: $(PROGRAM).elf
	$(OBJCOPY) $^ -O binary $@


$(PROGRAM).elf: $(OBJS)
	$(LD) $(LDFLAGS) -o $@ $^


clean:
	rm -f $(PROGRAM).bin $(PROGRAM).elf $(OBJS)


## Flash and debug targets

flash: $(PROGRAM).hex
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets -c "reset halt" \
                 -c "flash write_image erase $(PROGRAM).hex" -c "verify_image $(PROGRAM).hex" \
                 -c "reset run" -c shutdown


flash_s110: $(NRF_S110)/s110_nrf51822_7.0.0_softdevice.hex
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets -c "reset halt" \
                 -c "nrf51 mass_erase" \
                 -c "flash write_image erase s110/s110_nrf51822_7.0.0_softdevice.hex" \
                 -c "reset run" -c shutdown


flash_mbs: bootloaders/nrf_mbs_v1.0.hex
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets -c "reset halt" \
                 -c "flash write_image erase $^" -c "verify_image $^" -c "reset halt" \
	               -c "mww 0x4001e504 0x01" -c "mww 0x10001014 0x3F000" \
	               -c "reset run" -c shutdown


flash_cload: bootloaders/cload_nrf_v1.0.hex
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets -c "reset halt" \
                 -c "flash write_image erase $^" -c "verify_image $^" -c "reset halt" \
	               -c "mww 0x4001e504 0x01" -c "mww 0x10001014 0x3F000" \
	               -c "mww 0x4001e504 0x01" -c "mww 0x10001080 0x3A000" -c "reset run" -c shutdown

mass_erase:
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets -c "reset halt" \
                 -c "nrf51 mass_erase" -c shutdown


reset:
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets \
	               -c reset -c shutdown


openocd: $(PROGRAM).elf
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets




semihosting: $(PROGRAM).elf
	$(OPENOCD) -d2 -f $(OPENOCD_INTERFACE) -f $(OPENOCD_TARGET) -c init -c targets -c reset -c "arm semihosting enable" -c reset


gdb: $(PROGRAM).elf
	$(GDB) -ex "target remote localhost:3333" -ex "monitor reset halt" $^


flash_jlink:
	JLinkExe -if swd -device NRF51822 flash.jlink


cload: $(PROGRAM).bin
ifeq ($(strip $(S110)), 1)
	$(CLOAD_SCRIPT) flash $(PROGRAM).bin nrf51-fw
else
	@echo "Only S110 build can be bootloaded. Launch build and cload with S110=1"
endif


factory_reset:
	make mass_erase
ifeq ($(strip $(S110)),1)
	make flash_s110
	make flash_mbs
	make flash_cload
endif
	make flash

从上面的文件中,我们可以看到字样## Flash and debug targets下面基本上是跟下载有关系的命令行,正常情况下,只要在终端

make flash
make flash_s110
make flash_mbs
make flash_cload

就可以把对应的程序下载到nRF51822中,但是make flash之后会有如下提示错误:

bitcraze@bitcraze-vm:~/projects/crazyflie2-nrf-firmware$ make mass_erase
openocd -d2 -f interface/stlink-v2.cfg -f target/nrf51_stlink.tcl -c init -c targets -c "reset halt" \
                 -c "nrf51 mass_erase" -c shutdown
Open On-Chip Debugger 0.8.0 (2014-05-10-23:21)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.sourceforge.net/doc/doxygen/bugs.html
debug_level: 2
Info : This adapter doesn't support configurable speed
Info : STLINK v2 JTAG v16 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 2.559498
Info : nrf51.cpu: hardware has 4 breakpoints, 2 watchpoints
    TargetName         Type       Endian TapName            State       
--  ------------------ ---------- ------ ------------------ ------------
 0* nrf51.cpu          hla_target little nrf51.cpu          running
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0xc1000000 pc: 0x000006d0 msp: 0x000007c0
Warn : Unknown device (HWID 0x00000058)

最后总是提示Warn : Unknown device (HWID 0x00000058),很奇怪,网上各种搜,最后还是觉得肯定不是硬件连接没有问题,还是openocd没有吃透导致识别错误。使用Makefile无法走通,我们知道openocd本身是一个命令一个命令的尝试去做debug的工具,所以我们可以把Makefile中的命令行拆开,看看是哪里出了问题。首先打开一个终端,键入openocd -d2 -f interface/stlink-v2.cfg -f target/nrf51_stlink.tcl

bitcraze@bitcraze-vm:~/projects/crazyflie2-nrf-firmware$ openocd -d2 -f interface/stlink-v2.cfg -f target/nrf51_stlink.tcl
Open On-Chip Debugger 0.8.0 (2014-05-10-23:21)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.sourceforge.net/doc/doxygen/bugs.html
debug_level: 2
Info : This adapter doesn't support configurable speed
Info : STLINK v2 JTAG v16 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 2.556986
Info : nrf51.cpu: hardware has 4 breakpoints, 2 watchpoints

然后再打开另外一个终端,键入telnet localhost 4444:

bitcraze@bitcraze-vm:~$ telnet localhost 4444
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger
> 

然后继续输入reset和halt

> reset
> halt
target state: halted
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0xc1000003 pc: 0xfffffffe msp: 0xffffffd8

下面我们该擦除nRF51822里面的程序,下载新的程序:

> nrf51 mass_erase

有的时候这里依然会提示Unknown device (HWID 0x00000058),但是再尝试输入一次nrf51 mass_erase可能就没有这个问题了,之后可以开始下载mbs,cload,s110和firmwire的程序了,这里我们进行了分别下载,ST-LINK速度超慢。

> flash write_image erase bootloaders/nrf_mbs_v1.0.hex
auto erase enabled
wrote 3072 bytes from file bootloaders/nrf_mbs_v1.0.hex in 19.444714s (0.154 KiB/s)
> flash write_image erase bootloaders/cload_nrf_v1.0.hex         
auto erase enabled
wrote 13312 bytes from file bootloaders/cload_nrf_v1.0.hex in 87.047249s (0.149 KiB/s)
> flash write_image erase s110/s110_nrf51822_7.0.0_softdevice.hex
auto erase enabled
Padding image section 0 with 2112 bytes
> flash write_image erase cf2_nrf.hex
auto erase enabled
wrote 45056 bytes from file cf2_nrf.hex in 286.025299s (0.154 KiB/s)
> 
 
 
 
 

从上面来看,所有的程序都已经下载OK。另外从Makefile最后的部分可以看到是支持jlink下载的,这部分之后会尝试下

flash_jlink:
	JLinkExe -if swd -device NRF51822 flash.jlink

你可能感兴趣的:(xubuntu openocd nRF51822 download)