ToB企服应用市场:ToB评测及商务社交产业平台

标题: gcc-arm+make+openocd+vscode单片机开发环境搭建 [打印本页]

作者: 刘俊凯    时间: 2024-8-13 03:41
标题: gcc-arm+make+openocd+vscode单片机开发环境搭建
一、安装包下载与安装

1.gcc-arm

1.下载

arm-gnu-none-eabi
  找到对应平台下的arm工具链进行下载,支持Windows、Linux、Mac OS系统。

2.安装

  arm工具链的安装极其简单,这里不再赘述。安装完成后将gcc-arm目录下的文件夹参加到环境变量中。
   gcc-arm-none-eabi-10_2021.10\bin
gcc-arm-none-eabi-10_2021.10\arm-none-eabi\bin
  2.make

  使用cygwin来进行下载make。
  下载和安装参考以下博客:
windows环境下gcc的使用(一):安装cygwin
注意: 只安装make即可,不需要安装博客中提到的那些内容。
  安装完成后将以下目录参加到系统环境变量:
   cygwin64\bin
cygwin64\sbin
  3.openocd

1.下载

Download pre-built OpenOCD for Windows

2.安装

  openocd下载下来是一个以.7z末端的压缩文件,7z压缩文件的解压请下载7-Zip工具。
  解压后还需要将openocd的以下目录参加到系统环境变量:
   OpenOCD-20231002-0.12.0\bin
  4.vscode

  下载和安装请参考博客VSCode安装配置使用教程(最新版超详细保姆级含插件)一文就够了
二、VSCode的配置

1.插件安装

1.须要的插件



2.推荐插件



2.配置

  注意在 vscode 中打开文件夹后需要在当前文件夹下自行创建一个 .vscode 文件夹(或者执行以下恣意操纵 vscode 将自动创建 .vscode 文件夹)。
1.c_cpp_properties.json

  使用快捷键 ctrl+shift+p 输入c/c++ui 进入 C/C++:Edit Configurations(UI)。

  需要做以下配置:

  配置完成后vscode将在.vscode文件夹下创建 c_cpp_properties.json 文件。

2.tasks.json

  1.点击终端下的配置使命。

  2.选择 使用模板创建 tasks.json 。

  3.选择 MSBuild ,将在 .vscode 文件夹下创建 tasks.json 文件。


  4.修改以上文件内容为:
  1. {
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558
  3.     // for the documentation about the tasks.json format
  4.     "version": "2.0.0",
  5.     "tasks": [
  6.         {
  7.             "label": "build",
  8.             "type": "shell",
  9.             "command": "make",
  10.             "args": [
  11.                 "-C",
  12.                 "./Debug", // 目录
  13.                 "-j12"
  14.             ],
  15.             "group": "build",
  16.         },
  17.         {
  18.             "label": "clean",
  19.             "type": "shell",
  20.             "command": "make",
  21.             "args": [
  22.                 "-C",
  23.                 "./Debug",
  24.                 "clean"
  25.             ],
  26.             "group": "build",
  27.         },
  28.         {
  29.             "label": "download",
  30.             "type": "shell",
  31.             "command": "openocd",
  32.             "args": [
  33.                 "-f",
  34.                 "./Debug/stlink.cfg",
  35.                 "-f",
  36.                 "./Debug/stm32f1x.cfg",
  37.                 "-c",
  38.                 "program Debug/gcc-test.elf verify reset exit"
  39.             ],
  40.             "group": "build",
  41.         }
  42.     ]
  43. }
复制代码
  打开 终端 -> 运行使命… 就可以看到刚刚配置的命令了。


3.launch.json

  1.创建 launch.json 文件。



  2.修改 launch.json 文件。
  1. {
  2.     // 使用 IntelliSense 了解相关属性。
  3.     // 悬停以查看现有属性的描述。
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.     "version": "0.2.0",
  6.     "configurations": [
  7.         {
  8.             "cwd": "${workspaceRoot}",
  9.             "executable": "./Debug/gcc-test.elf",
  10.             "name": "Debug Microcontroller",
  11.             "request": "launch",
  12.             "type": "cortex-debug",
  13.             //"showDevDebugOutput": false,
  14.             "servertype": "openocd",
  15.             // .svd 文件可以在 keil 文件夹 Local\Arm\Packs\Keil\单片机_DFP\版本号\SVD 文件夹下找
  16.             "svdFile": "./Debug/STM32F103xx.svd",
  17.             "configFiles": [
  18.             // 单片机.cfg 文件在 share\openocd\scripts\target
  19.             // 仿真器.cfg 文件在 share\openocd\scripts\interface
  20.                 "./Debug/stlink.cfg",
  21.                 "./Debug/stm32f1x.cfg"
  22.             ]
  23.         }
  24.     ]
  25. }
复制代码
三、Makefile文件

  我参考 STM32cube MX 自动天生的 Makefile 进行修改得到以下 Makefile :
  1. # ------------------------------------------------
  2. # Makefile
  3. # REFER : STM32Cube MX automatically generated Makefile file
  4. #
  5. # ChangeLog :
  6. #   2024-08-11 - first version
  7. # ------------------------------------------------
  8. #目标文件名
  9. TARGET = gcc-test
  10. ######################################
  11. # building variables
  12. ######################################
  13. # debug build?
  14. DEBUG = 1
  15. # optimization
  16. OPT = -Og
  17. #######################################
  18. # paths
  19. #######################################
  20. # Build path
  21. BUILD_DIR = build
  22. OUTPUT_DIR = .
  23. # C源文件
  24. C_SOURCES +=  $(wildcard \
  25. ../APP/*.c \
  26. ../CORE/*.c \
  27. ../STM32_Lib/conf/*.c \
  28. ../STM32_Lib/src/*.c \
  29. )
  30. # ASM 源文件
  31. ASM_SOURCES =  \
  32. ../CORE/startup_stm32f10x_hd.s
  33. #######################################
  34. # binaries
  35. #######################################
  36. PREFIX = arm-none-eabi-
  37. # The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
  38. # either it can be added to the PATH environment variable.
  39. ifdef GCC_PATH
  40. CC = $(GCC_PATH)/$(PREFIX)gcc
  41. AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
  42. CP = $(GCC_PATH)/$(PREFIX)objcopy
  43. SZ = $(GCC_PATH)/$(PREFIX)size
  44. else
  45. CC = $(PREFIX)gcc
  46. AS = $(PREFIX)gcc -x assembler-with-cpp
  47. CP = $(PREFIX)objcopy
  48. SZ = $(PREFIX)size
  49. endif
  50. HEX = $(CP) -O ihex
  51. BIN = $(CP) -O binary -S
  52. #######################################
  53. # CFLAGS
  54. #######################################
  55. # cpu
  56. CPU = -mcpu=cortex-m3
  57. # fpu
  58. # NONE for Cortex-M0/M0+/M3
  59. # float-abi
  60. # mcu
  61. MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
  62. # macros for gcc
  63. # AS defines
  64. AS_DEFS =
  65. # 宏定义
  66. C_DEFS =  \
  67. -DUSE_STDPERIPH_DRIVER \
  68. -DSTM32F10X_HD
  69. # AS includes
  70. AS_INCLUDES =
  71. # C 头文件目录
  72. C_INCLUDES =  \
  73. -I../CORE \
  74. -I../STM32_Lib/conf \
  75. -I../STM32_Lib/inc
  76. # compile gcc flags
  77. ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=c99
  78. CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=c99
  79. ifeq ($(DEBUG), 1)
  80. CFLAGS += -g -gdwarf-2
  81. endif
  82. # Generate dependency information
  83. CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
  84. # 链接脚本
  85. LDSCRIPT = ../Link/STM32F103ZETx_FLASH.ld
  86. # libraries
  87. LIBS = -lc -lm -lnosys
  88. LIBDIR =
  89. LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(OUTPUT_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
  90. # default action: build all
  91. all: $(OUTPUT_DIR)/$(TARGET).elf $(OUTPUT_DIR)/$(TARGET).hex $(OUTPUT_DIR)/$(TARGET).bin
  92. #######################################
  93. # build the application
  94. #######################################
  95. # list of objects
  96. OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
  97. vpath %.c $(sort $(dir $(C_SOURCES)))
  98. # list of ASM program objects
  99. OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
  100. vpath %.s $(sort $(dir $(ASM_SOURCES)))
  101. $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
  102.         $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
  103. $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
  104.         $(AS) -c $(CFLAGS) $< -o $@
  105. $(OUTPUT_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
  106.         $(CC) $(OBJECTS) $(LDFLAGS) -o $@
  107.         $(SZ) $@
  108. $(OUTPUT_DIR)/%.hex: $(OUTPUT_DIR)/%.elf | $(BUILD_DIR)
  109.         $(HEX) $< $@
  110.        
  111. $(OUTPUT_DIR)/%.bin: $(OUTPUT_DIR)/%.elf | $(BUILD_DIR)
  112.         $(BIN) $< $@       
  113. #######################################
  114. # create build directory
  115.        
  116. $(BUILD_DIR):
  117.         mkdir $@               
  118. #######################################
  119. # clean up
  120. #######################################
  121. clean:
  122.         -rm -fR $(BUILD_DIR)
  123.         -rm -fR $(OUTPUT_DIR)/*.bin $(OUTPUT_DIR)/*.hex $(OUTPUT_DIR)/*.elf $(OUTPUT_DIR)/*.map
  124.   
  125. #######################################
  126. # dependencies
  127. #######################################
  128. -include $(wildcard $(BUILD_DIR)/*.d)
  129. # *** EOF ***
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4