Linux下STM32开发(3)——固件库工程编译与下载(Makefile)

一、环境搭建与基础

Linux下STM32开发(1)——编译和烧录环境搭建

Linux下STM32开发(2)——简单工程编写、编译与下载(Makefile)

 

二、创建工程

1、工程结构如下:  

.
├── Doc
│   └── readme.txt
├── Libraries
│   ├── CMSIS
│   │   ├── core_cm3.h
│   │   ├── startup
│   │   │   ├── startup_stm32f10x_cl.s
│   │   │   ├── startup_stm32f10x_hd.s
│   │   │   ├── startup_stm32f10x_hd_vl.s
│   │   │   ├── startup_stm32f10x_ld.s
│   │   │   ├── startup_stm32f10x_ld_vl.s
│   │   │   ├── startup_stm32f10x_md.s
│   │   │   ├── startup_stm32f10x_md_vl.s
│   │   │   └── startup_stm32f10x_xl.s
│   │   ├── stm32f10x.h
│   │   ├── system_stm32f10x.c
│   │   └── system_stm32f10x.h
│   └── FWlib
│       ├── inc
│       │   ├── misc.h
│       │   ├── stm32f10x_adc.h
│       │   ├── stm32f10x_bkp.h
│       │   ├── stm32f10x_can.h
│       │   ├── stm32f10x_cec.h
│       │   ├── stm32f10x_crc.h
│       │   ├── stm32f10x_dac.h
│       │   ├── stm32f10x_dbgmcu.h
│       │   ├── stm32f10x_dma.h
│       │   ├── stm32f10x_exti.h
│       │   ├── stm32f10x_flash.h
│       │   ├── stm32f10x_fsmc.h
│       │   ├── stm32f10x_gpio.h
│       │   ├── stm32f10x_i2c.h
│       │   ├── stm32f10x_iwdg.h
│       │   ├── stm32f10x_pwr.h
│       │   ├── stm32f10x_rcc.h
│       │   ├── stm32f10x_rtc.h
│       │   ├── stm32f10x_sdio.h
│       │   ├── stm32f10x_spi.h
│       │   ├── stm32f10x_tim.h
│       │   ├── stm32f10x_usart.h
│       │   └── stm32f10x_wwdg.h
│       └── src
│           ├── misc.c
│           ├── stm32f10x_adc.c
│           ├── stm32f10x_bkp.c
│           ├── stm32f10x_can.c
│           ├── stm32f10x_cec.c
│           ├── stm32f10x_crc.c
│           ├── stm32f10x_dac.c
│           ├── stm32f10x_dbgmcu.c
│           ├── stm32f10x_dma.c
│           ├── stm32f10x_exti.c
│           ├── stm32f10x_flash.c
│           ├── stm32f10x_fsmc.c
│           ├── stm32f10x_gpio.c
│           ├── stm32f10x_i2c.c
│           ├── stm32f10x_iwdg.c
│           ├── stm32f10x_pwr.c
│           ├── stm32f10x_rcc.c
│           ├── stm32f10x_rtc.c
│           ├── stm32f10x_sdio.c
│           ├── stm32f10x_spi.c
│           ├── stm32f10x_tim.c
│           ├── stm32f10x_usart.c
│           └── stm32f10x_wwdg.c
├── Makefile
├── stm32_flash.ld
└── User
    ├── led
    │   ├── bsp_led.c
    │   └── bsp_led.h
    ├── main.c
    ├── stm32f10x_conf.h
    ├── stm32f10x_it.c
    └── stm32f10x_it.h

来源:

文件夹/文件  
Libraries/CMSIS

stm32固件库的CMSIS支持库。

内核支持 core_cm3.h ,stm32头文件 stm32f10x.h 

时钟配置文件 system_stm32f10x.h 和 system_stm32f10x.c

从 STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3 复制

Libraries/CMSIS/startup

启动文件。

从 STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport

\ST\STM32F10x\startup\TrueSTUDIO 复制

Libraries/CMSIS/FWlib

外设库文件。

从 STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\STM32F10x_StdPeriph_Driver 复制

stm32_flash.ld

链接文件。

从 STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template\TrueSTUDIO 复制,再根据自己芯片修改。

Makefile 自己编写。
User

用户文件。

固件库配置头文件 stm32f10x_conf.h ,中断服务程序文件 stm32f10x_it.h 和 stm32f10x_it.c

从 STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template 复制

 

Makefile:

TARGET=test
CROSS_COMPILE   ?= arm-none-eabi-
CC         		:= $(CROSS_COMPILE)gcc
OBJCOPY         := $(CROSS_COMPILE)objcopy
RM=rm -f
CORE=3
CPUFLAGS=-mthumb -mcpu=cortex-m$(CORE)
#头文件路径
INCFLAGS=-I $(PWD)/Libraries/CMSIS -I$(PWD)/Libraries/FWlib/inc -I $(PWD)/User -I $(PWD)/User/led
#链接指令
LDFLAGS = -T stm32_flash.ld -Wl,-cref,-u,Reset_Handler -Wl,-Map=$(TARGET).map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 -Wl,--start-group -lc -lm -Wl,--end-group
#添加头文件路径 表明使用标准外设驱动库
CFLAGS=$(INCFLAGS) -D STM32F10X_HD -D USE_STDPERIPH_DRIVER -Wall -g
#烧录器配置文件路径
INTERRFACE_CFG=/usr/local/share/openocd/scripts/interface/stlink-v2.cfg
#目标芯片配置文件路径
TARGET_CFG=/usr/local/share/openocd/scripts/target/stm32f1x.cfg 
#当前路径
PWD=$(shell pwd)

#搜索启动文件
START_SRC=$(shell find ./ -name 'startup_stm32f10x_hd.s')
#定义目标集合,.s替换成.o文件
START_OBJ=$(START_SRC:%.s=%.o)

#搜索所有的.c文件
C_SRC=$(shell find ./ -name '*.c')
#定义一个目标集合,把搜索到的.c文件替换成.o文件
C_OBJ=$(C_SRC:%.c=%.o)

#链接 输出bin、hex
$(TARGET):$(START_OBJ) $(C_OBJ)
	$(CC) $^ $(CPUFLAGS) $(LDFLAGS) $(CFLAGS) -o $(TARGET).elf
	$(OBJCOPY) $(TARGET).elf $(TARGET).bin
	$(OBJCOPY) $(TARGET).elf -Oihex $(TARGET).hex

#编译
$(START_OBJ):$(START_SRC)
	$(CC) -c $^ $(CPUFLAGS) $(CFLAGS) -o $@
$(C_OBJ):%.o:%.c
	$(CC) -c $^ $(CPUFLAGS) $(CFLAGS) -o $@

clean:
	$(RM) $(shell find ./ -name '*.o') $(TARGET).*
	
download:
		openocd -f $(INTERRFACE_CFG) -f $(TARGET_CFG) -c init -c halt -c "flash write_image erase $(PWD)/$(TARGET).bin" -c reset -c shutdown

 

三、测试: 

使用 make 命令生成 .elf .map .bin .hex 文件

使用 make download 命令 stlink和芯片自动连接,烧录bin文件 

Linux下STM32开发(3)——固件库工程编译与下载(Makefile)_第1张图片

使用 make clean 命令清除 .elf .map .bin .hex 文件 

Linux下STM32开发(3)——固件库工程编译与下载(Makefile)_第2张图片

 

make后的工程目录:

.
├── Doc
│   └── readme.txt
├── Libraries
│   ├── CMSIS
│   │   ├── core_cm3.h
│   │   ├── startup
│   │   │   ├── startup_stm32f10x_cl.s
│   │   │   ├── startup_stm32f10x_hd.o
│   │   │   ├── startup_stm32f10x_hd.s
│   │   │   ├── startup_stm32f10x_hd_vl.s
│   │   │   ├── startup_stm32f10x_ld.s
│   │   │   ├── startup_stm32f10x_ld_vl.s
│   │   │   ├── startup_stm32f10x_md.s
│   │   │   ├── startup_stm32f10x_md_vl.s
│   │   │   └── startup_stm32f10x_xl.s
│   │   ├── stm32f10x.h
│   │   ├── system_stm32f10x.c
│   │   ├── system_stm32f10x.h
│   │   └── system_stm32f10x.o
│   └── FWlib
│       ├── inc
│       │   ├── misc.h
│       │   ├── stm32f10x_adc.h
│       │   ├── stm32f10x_bkp.h
│       │   ├── stm32f10x_can.h
│       │   ├── stm32f10x_cec.h
│       │   ├── stm32f10x_crc.h
│       │   ├── stm32f10x_dac.h
│       │   ├── stm32f10x_dbgmcu.h
│       │   ├── stm32f10x_dma.h
│       │   ├── stm32f10x_exti.h
│       │   ├── stm32f10x_flash.h
│       │   ├── stm32f10x_fsmc.h
│       │   ├── stm32f10x_gpio.h
│       │   ├── stm32f10x_i2c.h
│       │   ├── stm32f10x_iwdg.h
│       │   ├── stm32f10x_pwr.h
│       │   ├── stm32f10x_rcc.h
│       │   ├── stm32f10x_rtc.h
│       │   ├── stm32f10x_sdio.h
│       │   ├── stm32f10x_spi.h
│       │   ├── stm32f10x_tim.h
│       │   ├── stm32f10x_usart.h
│       │   └── stm32f10x_wwdg.h
│       └── src
│           ├── misc.c
│           ├── misc.o
│           ├── stm32f10x_adc.c
│           ├── stm32f10x_adc.o
│           ├── stm32f10x_bkp.c
│           ├── stm32f10x_bkp.o
│           ├── stm32f10x_can.c
│           ├── stm32f10x_can.o
│           ├── stm32f10x_cec.c
│           ├── stm32f10x_cec.o
│           ├── stm32f10x_crc.c
│           ├── stm32f10x_crc.o
│           ├── stm32f10x_dac.c
│           ├── stm32f10x_dac.o
│           ├── stm32f10x_dbgmcu.c
│           ├── stm32f10x_dbgmcu.o
│           ├── stm32f10x_dma.c
│           ├── stm32f10x_dma.o
│           ├── stm32f10x_exti.c
│           ├── stm32f10x_exti.o
│           ├── stm32f10x_flash.c
│           ├── stm32f10x_flash.o
│           ├── stm32f10x_fsmc.c
│           ├── stm32f10x_fsmc.o
│           ├── stm32f10x_gpio.c
│           ├── stm32f10x_gpio.o
│           ├── stm32f10x_i2c.c
│           ├── stm32f10x_i2c.o
│           ├── stm32f10x_iwdg.c
│           ├── stm32f10x_iwdg.o
│           ├── stm32f10x_pwr.c
│           ├── stm32f10x_pwr.o
│           ├── stm32f10x_rcc.c
│           ├── stm32f10x_rcc.o
│           ├── stm32f10x_rtc.c
│           ├── stm32f10x_rtc.o
│           ├── stm32f10x_sdio.c
│           ├── stm32f10x_sdio.o
│           ├── stm32f10x_spi.c
│           ├── stm32f10x_spi.o
│           ├── stm32f10x_tim.c
│           ├── stm32f10x_tim.o
│           ├── stm32f10x_usart.c
│           ├── stm32f10x_usart.o
│           ├── stm32f10x_wwdg.c
│           └── stm32f10x_wwdg.o
├── Makefile
├── stm32_flash.ld
├── test.bin
├── test.elf
├── test.hex
├── test.map
└── User
    ├── led
    │   ├── bsp_led.c
    │   ├── bsp_led.h
    │   └── bsp_led.o
    ├── main.c
    ├── main.o
    ├── stm32f10x_conf.h
    ├── stm32f10x_it.c
    ├── stm32f10x_it.h
    └── stm32f10x_it.o

 

参考:https://blog.csdn.net/Mculover666/article/details/84975991

你可能感兴趣的:(#)