收藏官网首页
查看: 12740|回复: 1

ubuntu linux下建立stm32开发环境: GCC安装以及工程Makefile建立

56

主题

144

帖子

1066

积分

金牌会员

Rank: 6Rank: 6

积分
1066
QQ
跳转到指定楼层
楼主
发表于 2016-12-2 16:08:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
注册成为机智云开发者,手机加虚拟设备快速开发
本帖最后由 Gemini 于 2017-1-3 15:10 编辑

买了一个STM32开发板,却不想在window下开发,也不想用那么占内存的IAR MDK等软件,所以决定在ubuntu下建立该开发环境,像之前avr linux一样,找了下资料,国内有人做过,但都没有很详尽的教程,所以花了三四天才完成.其实原理很简单,就是安装适用与STM32的GCC,以及建立该工程,主要是Makefile加上STM32的官方库.
个人原创,转载请注明原文出处:
参考:

环境:
ubuntu 13.10
stm32f103zet6
一  STM 32 GCC 安装
stm32 属于arm cortex-m系列thumb指令集,所以给arm用的arm-none-eabi就可以了,首先是下载
下载地址:
https://launchpad.net/gcc-arm-embedded/+download
下载其中的gcc-arm-none-eabi-version-linux.tar.bz2
解压到你知道的目录会产生 gcc-arm-none-eabi的文件夹
把该编译器添加到你的环境中:
  1. <font face="微软雅黑" size="3">sudo gedit  ~/.bashrc</font>
复制代码
在最后一行添加:
  1. <font face="微软雅黑" size="3">1
  2. export PATH=$PATH:/your_stm_gcc_dir/gcc-arm-none-eabi-4_8-2013q4/bin
  3. </font>
复制代码

因为我之前有添加过树莓派的编译器了,所以实际上是这样的:
  1. <font face="微软雅黑" size="3">export PATH=$PATH:/your_pi_gcc_dir/tools-master/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/:/your_stm_gcc_dir/gcc-arm-none-eabi-4_8-2013q4/bin
  2. </font>
复制代码

两个编译器环境中间用冒号隔开;
注销后测试:
  1. <font face="微软雅黑" size="3">arm-none-eabi-gcc -v</font>
复制代码

可以查看到该编译器的版本,就表示可以了.
二  工程环境的建立
新建个工程文件夹,及其目录
  1. <font face="微软雅黑" size="3">mkdir stm_project
  2. cd stm_project
  3. mkdir libs
  4. mkdir src
  5. mkdir inc</font>
复制代码

下载,安装官方库:
stm32的寄存器不像51 avr等单片机,那么少,自己写写库,背背寄存器就可以了,所以ST公司提供了他们官方的库,为了避免重复造轮子,就直接采用他们的库,库版本为STM32_USB-FS-Device_Lib_V4.0.0,这个库多了usb支持,下载的话到st官网搜索stm32f10x就有了.

下载链接:

解压,把解压好的文件夹复制到刚才新建的libs里面.
在工程根目录下新建Makefile.common文件,这个为通用makefile
  1. <font face="微软雅黑" size="3"># include Makefile

  2. #This file is included in the general Makefile, the libs Makefile and the src Makefile
  3. #Different optimize settings for library and source files can be realized by using arguments
  4. #Compiler optimize settings:
  5. # -O0 no optimize, reduce compilation time and make debugging produce the expected results (default).
  6. # -O1 optimize, reduce code size and execution time, without much increase of compilation time.
  7. # -O2 optimize, reduce code execution time compared to ‘O1’, increase of compilation time.
  8. # -O3 optimize, turns on all optimizations, further increase of compilation time.
  9. # -Os optimize for size, enables all ‘-O2’ optimizations that do not typically increase code size and other code size optimizations.
  10. #Recommended optimize settings for release version: -O3
  11. #Recommended optimize settings for debug version: -O0
  12. #Valid parameters :
  13. # OptLIB=0 --> optimize library files using the -O0 setting
  14. # OptLIB=1 --> optimize library files using the -O1 setting
  15. # OptLIB=2 --> optimize library files using the -O2 setting
  16. # OptLIB=3 --> optimize library files using the -O3 setting
  17. # OptLIB=s --> optimize library files using the -Os setting
  18. # OptSRC=0 --> optimize source files using the -O0 setting
  19. # OptSRC=1 --> optimize source files using the -O1 setting
  20. # OptSRC=2 --> optimize source files using the -O2 setting
  21. # OptSRC=3 --> optimize source files using the -O3 setting
  22. # OptSRC=s --> optimize source files using the -Os setting
  23. # all --> build all
  24. # libs --> build libs only
  25. # src --> build src only
  26. # clean --> clean project
  27. # tshow --> show optimize settings
  28. #Example:
  29. # make OptLIB=3 OptSRC=0 all tshow

  30. TOP=$(shell readlink -f "$(dir $(lastword $(MAKEFILE_LIST)))")
  31. PROGRAM=main
  32. LIBDIR=$(TOP)/libs

  33. #Adust the following line to the library in use
  34. #=========add by embbnux 根据你的库不同,调整这个地方的库目录地址====================#
  35. STMLIB=$(LIBDIR)/STM32_USB-FS-Device_Lib_V4.0.0/Libraries
  36. #=========add by embbnux 根据你的stm32芯片型号容量不同,修改这个地方的TypeOfMCU=======#
  37. #Adjust TypeOfMCU in use, see CMSIS file "stm32f10x.h"#STM32F103RBT (128KB FLASH, 20KB RAM) --> STM32F10X_MD#TypeOfMCU=STM32F10X_MD#STM32F103RET (512KB FLASH, 64KB RAM) --> STM32F10X_HD#STM32F103ZET (512KB FLASH, 64KB RAM) --> STM32F10X_HD
  38. #============================================================================#
  39. TypeOfMCU=STM32F10X_HD
  40. #============================================================================#
  41. TC=arm-none-eabi
  42. CC=$(TC)-gcc
  43. LD=$(TC)-ld -v
  44. OBJCOPY=$(TC)-objcopy
  45. AR=$(TC)-ar
  46. GDB=$(TC)-gdb
  47. INCLUDE=-I$(TOP)/inc
  48. INCLUDE+=-I$(STMLIB)/CMSIS/Include
  49. INCLUDE+=-I$(STMLIB)/CMSIS/Device/ST/STM32F10x/Include
  50. INCLUDE+=-I$(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates
  51. INCLUDE+=-I$(STMLIB)/STM32F10x_StdPeriph_Driver/inc
  52. INCLUDE+=-I$(STMLIB)/STM32_USB-FS-Device_Driver/inc
  53. COMMONFLAGS=-g -mcpu=cortex-m3 -mthumb
  54. COMMONFLAGSlib=$(COMMONFLAGS)
  55. #Commands for general Makefile and src Makefile
  56. ifeq ($(OptSRC),0)
  57.   COMMONFLAGS+=-O0
  58.   InfoTextSrc=src (no optimize, -O0)
  59. else ifeq ($(OptSRC),1)
  60.   COMMONFLAGS+=-O1
  61.   InfoTextSrc=src (optimize time+ size+, -O1)
  62. else ifeq ($(OptSRC),2)
  63.   COMMONFLAGS+=-O2
  64.   InfoTextSrc=src (optimize time++ size+, -O2)
  65. else ifeq ($(OptSRC),s)
  66.     COMMONFLAGS+=-Os
  67.    InfoTextSrc=src (optimize size++, -Os)
  68. else
  69.   COMMONFLAGS+=-O3
  70.   InfoTextSrc=src (full optimize, -O3)
  71. endif
  72. CFLAGS+=$(COMMONFLAGS) -Wall -Werror $(INCLUDE)
  73. CFLAGS+=-D $(TypeOfMCU)
  74. CFLAGS+=-D VECT_TAB_FLASH

  75. #Commands for libs Makefile
  76. ifeq ($(OptLIB),0)
  77.   COMMONFLAGSlib+=-O0
  78.   InfoTextLib=libs (no optimize, -O0)
  79. else ifeq ($(OptLIB),1)
  80.   COMMONFLAGSlib+=-O1
  81.   InfoTextLib=libs (optimize time+ size+, -O1)
  82. else ifeq ($(OptLIB),2)
  83.   COMMONFLAGSlib+=-O2
  84.   InfoTextLib=libs (optimize time++ size+, -O2)
  85. else ifeq ($(OptLIB),s)
  86.   COMMONFLAGSlib+=-Os
  87.   InfoTextLib=libs (optimize size++, -Os)
  88. else
  89.   COMMONFLAGSlib+=-O3
  90.   InfoTextLib=libs (full optimize, -O3)
  91. endif
  92. CFLAGSlib+=$(COMMONFLAGSlib) -Wall -Werror $(INCLUDE)
  93. CFLAGSlib+=-D $(TypeOfMCU)
  94. CFLAGSlib+=-D VECT_TAB_FLASH</font>
复制代码
编译该库:
  1. <font face="微软雅黑" size="3">make clean
  2. make</font>
复制代码

就会在lib目录下生成libstm32.a,这个就是编译好的静态库了.

  建立工程编译ld文件
这个ld文件,为在编译时告诉编译器把代码放到什么地址,根据芯片的内存以及flash容量不同有所调整
在工程根目录下新建linker.ld文件
代码较长,请到我的资源里面下载,或者查看参考pdf里面的:
也可到git上下载整个工程: stm32_development_on_linux

这里说明需要修改的地方,根据芯片型号不同,选择相应的RAM FLASH大小
  1. <font face="微软雅黑" size="3">MEMORY {
  2. /*Adust LENGTH to RAMsize of target MCU:*/
  3. /*STM32F103RBT --> 20K*/
  4. /*RAM (RWX) : ORIGIN = 0x20000000 , LENGTH = 20K*/
  5. /*STM32F103RET --> 64K*/
  6. /*STM32F103ZET --> 64K*/
  7. RAM (RWX) : ORIGIN = 0x20000000 , LENGTH = 64K
  8. EXTSRAM (RWX) : ORIGIN = 0x68000000 , LENGTH = 0
  9. /*Adust LENGTH to (FLASHsize - FeePROMsize) of target MCU:*/
  10. /*STM32F103RBT --> 126K*/
  11. FLASH (RX) : ORIGIN = 0x08000000 , LENGTH = 126K
  12. /*STM32F103RET --> 508K*/
  13. /*FLASH (RX) : ORIGIN = 0x08000000 , LENGTH = 508K*/
  14. /*STM32F103ZET --> 508K*/
  15. FLASH (RX) : ORIGIN = 0x08000000 , LENGTH = 508K
  16. /*Adust ORIGIN to (0x08000000 + (FLASHsize-FeePROMsize)) of target MCU*/
  17. /*and adust LENGTH to FeePROMsize allocated:*/
  18. /*STM32F103RBT --> 0x08000000+126K, 2K*/
  19. EEMUL (RWX) : ORIGIN = 0x08000000+126K, LENGTH = 2K
  20. /*STM32F103RET --> 0x08000000+508K, 4K*/
  21. /*EEMUL (RWX) : ORIGIN = 0x08000000+508K, LENGTH = 4K*/
  22. }</font>
复制代码
在工程根目录下新建Makefile文件:
  1. <font face="微软雅黑" size="3"># general Makefile

  2. include Makefile.common
  3. LDFLAGS=$(COMMONFLAGS) -fno-exceptions -ffunction-sections -fdata-sections -L$(LIBDIR) -nostartfiles -Wl,--gc-sections,-Tlinker.ld

  4. LDLIBS+=-lm
  5. LDLIBS+=-lstm32

  6. STARTUP=startup.c

  7. all: libs src
  8. $(CC) -o $(PROGRAM).elf $(LDFLAGS) \
  9. -Wl,--whole-archive \
  10. src/app.a \
  11. -Wl,--no-whole-archive \
  12. $(LDLIBS)
  13. $(OBJCOPY) -O ihex $(PROGRAM).elf $(PROGRAM).hex
  14. $(OBJCOPY) -O binary $(PROGRAM).elf $(PROGRAM).bin
  15. #Extract info contained in ELF to readable text-files:
  16. arm-none-eabi-readelf -a $(PROGRAM).elf > $(PROGRAM).info_elf
  17. arm-none-eabi-size -d -B -t $(PROGRAM).elf > $(PROGRAM).info_size
  18. arm-none-eabi-objdump -S $(PROGRAM).elf > $(PROGRAM).info_code
  19. arm-none-eabi-nm -t d -S --size-sort -s $(PROGRAM).elf > $(PROGRAM).info_symbol

  20. .PHONY: libs src clean tshow

  21. libs:
  22. $(MAKE) -C libs $@
  23. src:
  24. $(MAKE) -C src $@
  25. clean:
  26. $(MAKE) -C src $@
  27. $(MAKE) -C libs $@
  28. rm -f $(PROGRAM).elf $(PROGRAM).hex $(PROGRAM).bin $(PROGRAM).info_elf $(PROGRAM).info_size
  29. rm -f $(PROGRAM).info_code
  30. rm -f $(PROGRAM).info_symbol
  31. tshow:
  32. @echo "######################################################################################################"
  33. @echo "################# optimize settings: $(InfoTextLib), $(InfoTextSrc)"
  34. @echo "######################################################################################################"
  35. </font>
复制代码
差不多就好了,在src里面添加测试源码
主要是startup.c 以及main.c,这里就不在说明了,可以查看该参考资料的pdf或者到我的资源下载

src目录下新建文件Makefile:
  1. <font face="微软雅黑" size="3"># src Makefile

  2. include ../Makefile.common

  3. OBJS+=startup.o
  4. OBJS+=main.o

  5. all: src

  6. src: app.a

  7. app.a: $(OBJS)
  8. $(AR) cr app.a $(OBJS)
  9. .PHONY: src clean tshow

  10. clean:
  11. rm -f app.a $(OBJS)
  12. tshow:
  13. @echo "######################################################################################################"
  14. @echo "################# optimize settings: $(InfoTextLib), $(InfoTextSrc)"
  15. @echo "######################################################################################################"
  16. </font>
复制代码
然后进入工程主目录,下make就好了.
  1. <font face="微软雅黑" size="3">make clean
  2. make OptLIB=0 OptSRC=0 all tshow</font>
复制代码

然后,就完成了.

14

主题

105

帖子

846

积分

高级会员

Rank: 4

积分
846
沙发
发表于 2016-12-3 10:25:16 | 只看该作者
好帖好帖好帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表