Ubuntu16.04,18.04,20.04下的stm32环境配置

arm已经提供了现成的arm-none-eabi开发工具链, 只需要在项目中写好Makefile就可以了.

Ubuntu 16.04

必须安装libusb-1.0-0-dev, 其他安装不起作用

sudo apt-get install libusb-1.0-0-dev
git clone https://github.com/texane/stlink.git
cd stlink/
make clean
make
cd build/Release/
sudo make install

安装openocd

tar zxvf openocd-0.10.0.tar.gz
cd openocd-0.10.0/
./configure
make
sudo make install

Update 2018-05-11: 在Ubuntu18.04下, 无法使用tar包编译安装, 如果带 --enable-stlink会报错误 configure: error: libusb-1.x is required for the ST-Link JTAG Programmer, 但是不带这个参数一样无法通过编译, 经apt-cache show openocd发现版本是 0.10.0-4, 于是直接通过apt-get install安装

安装 gcc-arm-none-eabi

gcc-arm-none-eabi是一个开源的ARM开发工具链,适用于Arm Cortex-M和Coretex-A系列处理器,包括GNU编译器(GCC),以及GDB,可用于Windows,Linux,MacOS上的交叉编译。gcc-arm-none-eabi在ubuntu软件源仓库中就有,但是版本比较陈旧, 一般从ARM官方下载最新的版本安装

tar xvf gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2
cd /opt/
sudo mkdir gcc-arm
cd gcc-arm/
sudo mv ~/Backup/linux/gcc-arm-none-eabi-5_4-2016q3/ .
sudo chown -R root:root gcc-arm-none-eabi-5_4-2016q3/
/opt/gcc-arm/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gcc -v

Update 2018-05-11: 在Ubuntu18.04下, 自带的版本是 Version: 15:6.3.1+svn253039-1build1, 这个应该是比2016q4还更新的版本, 从官方下载的版本是 https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/7-2017q4/gcc-arm-none-eabi-7-2017-q4-major-linux.tar.bz2 . 使用2017q4这个版本编译安装后工作正常

将gcc-arm-none-eabi executables添加到PATH

edit ~/.profile, edit the last line

PATH="$HOME/bin:$HOME/.local/bin:/opt/gcc-arm/gcc-arm-none-eabi-5_4-2016q3/bin/:$PATH"
then run: source .prifle to make it take effect

硬件连接

st-link pin to STM32F103C8T6 mini board(SWD)

GND     G
SWCLK   CLK
SWDIO   IO
3.3V    V3

连接到PC后的dmesg输出

[10371.046367] usb 2-1.2: new full-speed USB device number 3 using ehci-pci
[10371.157440] usb 2-1.2: New USB device found, idVendor=0483, idProduct=3748
[10371.157448] usb 2-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[10371.157453] usb 2-1.2: Product: STM32 STLink
[10371.157457] usb 2-1.2: Manufacturer: STMicroelectronics
[10371.157461] usb 2-1.2: SerialNumber: VÿkIfHV'Fg

编译和写入

makefile 格式参考

link script, 例如这边用到的stm32_f103ze_gcc.ld, 可以在官方Libraries的Project Template下面找到例子.

例子

#工程的名称及最后生成文件的名字
TARGET = test001

#设定临时性环境变量
export CC             = arm-none-eabi-gcc           
export AS             = arm-none-eabi-as
export LD             = arm-none-eabi-ld
export OBJCOPY        = arm-none-eabi-objcopy

#读取当前工作目录
TOP=$(shell pwd)

#设定包含文件目录
INC_FLAGS= -I $(TOP)/core \
           -I $(TOP)/stm32f10x_lib/inc \
           -I $(TOP)/system \
           -I $(TOP)/user

CFLAGS =  -W -Wall -g -mcpu=cortex-m3 -mthumb -D STM32F10X_HD -D USE_STDPERIPH_DRIVER $(INC_FLAGS) -O0 -std=gnu11
C_SRC=$(shell find ./ -name '*.c')  
C_OBJ=$(C_SRC:%.c=%.o)          

.PHONY: all clean update      

all:$(C_OBJ)
	$(CC) $(C_OBJ) -T stm32_f103ze_gcc.ld -o $(TARGET).elf   -mthumb -mcpu=cortex-m3 -Wl,--start-group -lc -lm -Wl,--end-group -specs=nano.specs -specs=nosys.specs -static -Wl,-cref,-u,Reset_Handler -Wl,-Map=Project.map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 
	$(OBJCOPY) $(TARGET).elf  $(TARGET).bin -Obinary 
	$(OBJCOPY) $(TARGET).elf  $(TARGET).hex -Oihex

$(C_OBJ):%.o:%.c
	$(CC) -c $(CFLAGS) -o $@ $<
clean:
	rm -f $(shell find ./ -name '*.o')
	rm -f $(shell find ./ -name '*.d')
	rm -f $(shell find ./ -name '*.map')
	rm -f $(shell find ./ -name '*.elf')
	rm -f $(shell find ./ -name '*.bin')
	rm -f $(shell find ./ -name '*.hex')

update:
	openocd \
        -f /usr/share/openocd/scripts/interface/stlink-v2.cfg  \
        -f /usr/share/openocd/scripts/target/stm32f1x.cfg \
        -c init \
        -c halt \
        -c "flash write_image erase $(TOP)/test001.hex" \
        -c reset \
        -c shutdown

编译

# 在项目目录下
make clean
make
make update

写入实际执行的命令

$:~/ArmProjects/simple-gcc-stm32-project$ openocd -f /usr/local/share/openocd/scripts/interface/stlink-v2.cfg  -f /usr/local/share/openocd/scripts/target/stm32f1x.cfg -c init -c halt -c "flash write_image erase LED_project.hex" -c reset -c shutdown
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v21 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.256122
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
target halted due to debug-request, current mode: Thread
xPSR: 0x61000000 pc: 0x0800026e msp: 0x200003f0
auto erase enabled
Info : device id = 0x20036410
Info : flash size = 64kbytes
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000003a msp: 0x200003f0
wrote 7168 bytes from file LED_project.hex in 0.431739s (16.213 KiB/s)
shutdown command invoked

Ubuntu 20.04

前往ARM Developer 下载最新的gnu-toolchain

命令行历史

# 安装openocd
sudo apt install openocd
# 下载最新的gcc-arm-none-eabi, 而不是通过apt install gcc-arm-none-eabi安装
tar xvf gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 
cd /opt/
sudo mkdir gcc-arm
cd gcc-arm/
sudo mv ~/Backup/linux/gcc-arm-none-eabi-10-2020-q4-major .
sudo chown -R root:root gcc-arm-none-eabi-10-2020-q4-major/
./gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc -v
# 将gcc-arm-none-eabi加入路径
vi ~/.profile 
source .profile 
echo $PATH
arm-none-eabi-gcc -v

参考

posted on 2018-04-17 03:42  Milton  阅读(3456)  评论(0编辑  收藏  举报

导航