[活动] 极海APM32F411V Tiny开发板评测 GCC Makefile Pyocd 工程模板建立

[复制链接]
 楼主| lemonhub 发表于 2024-5-25 08:16 | 显示全部楼层 |阅读模式
极海APM32F411V Tiny开发板评测 GCC Makefile Pyocd 工程模板建立一、移植前的准备1. 准备目标硬件(开发板/芯片/模组)
本教程将使用极海APM32F411V Tiny开发板进行示例移植。调试ARM Cortex M核还需要仿真器,如果您的开发板或者芯片模组没有板载仿真器,就需要连接外置的仿真器,如DAPLink之类的。
apm32.jpg
2.准备编译器环境
1)Windows or Linux (Windows,linux基本环境流程差不多,可能需要修改部分Makefile代码)
2)Jlink、Daplink、Wch-Link烧录器
3)GNU Arm Embedded Toolchain交叉编译器
4)Mingw-w64 GCC for Windows 64
3.Windows环境搭建GNU Arm Embedded Toolchain交叉编译器安装
进入arm开发者官网,往下滑动选择下载解压可用的ZIP压缩包文件
下载链接: Downloads | GNU Arm Embedded Toolchain Downloads – Arm Developer
apm32-gcc1.png
将下载好的压缩包文件解压在gcc-arm-none-eabi”文件夹中,并记住文件内“bin”文件的路径,后续需添加到系统环境变量Path中。
apm32-gcc2.png
apm32-gcc3.png
添加完环境变量后,进行测试,检测是否安装好。
apm32-gcc4.png
安装Mingw-w64 GCC
点击链接进入到SourceForge官网,往下翻可以找到很多版本的下载链接,选择红色框内型号即可,不同前后缀的具体差异请参考:
MinGW gcc下载链接及sjlj、dwarf、seh异同以及gcc安装AMDDMA的博客-CSDN博客seh和sjlj
下载链接:
MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net
apm32-gcc5.png
同样,将下载好的文件解压到“gcc-arm-none-eabi”文件夹下,记住目录下的“bin”文件路径
apm32-gcc6.png
进入“bin”文件内找到“mingw32-make”应用程序文件,复制一份并重命名为“make”。这么做有利于在命令行执行make指令,而不是输入mingw32-make。
apm32-gcc7.png
添加完环境变量后,进行测试,检测是否安装好。
apm32-gcc8.png
apm32-gcc9.png
                     
3.创建工程,编写Makefile
  • 工程结构
    apm32-gcc10.jpg
  • Makefile

      
  1. ######################################
  2. # target
  3. ######################################
  4. TARGET = apm32f411_gcc_template


  5. ######################################
  6. # building variables
  7. ######################################
  8. # debug build?
  9. DEBUG = 1
  10. # optimization
  11. OPT = -Og


  12. #######################################
  13. # paths
  14. #######################################
  15. # Build path
  16. BUILD_DIR = output

  17. ######################################
  18. # source
  19. ######################################
  20. # C sources
  21. C_SOURCES = \
  22. Application/Source/main.c \
  23. Application/Source/apm32f4xx_int.c \
  24. Application/Source/system_apm32f4xx.c \
  25. Boards/Board_APM32F411_TINY.c \
  26. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_adc.c \
  27. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_can.c \
  28. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_crc.c \
  29. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_cryp.c \
  30. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_cryp_aes.c \
  31. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_cryp_des.c \
  32. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_cryp_tdes.c \
  33. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_dac.c \
  34. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_dbgmcu.c \
  35. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_dci.c \
  36. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_dma.c \
  37. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_dmc.c \
  38. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_eint.c \
  39. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_fmc.c \
  40. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_gpio.c \
  41. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_hash.c \
  42. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_hash_md5.c \
  43. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_hash_sha1.c \
  44. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_i2c.c \
  45. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_iwdt.c \
  46. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_misc.c \
  47. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_pmu.c \
  48. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_rcm.c \
  49. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_rng.c \
  50. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_rtc.c \
  51. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_sdio.c \
  52. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_smc.c \
  53. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_spi.c \
  54. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_syscfg.c \
  55. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_tmr.c \
  56. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_usart.c \
  57. Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_wwdt.c

  58. # ASM sources
  59. ASM_SOURCES = \
  60. Tools/startup_apm32f411.s


  61. #######################################
  62. # binaries
  63. #######################################
  64. PREFIX = arm-none-eabi-
  65. GCC_PATH = /SoftwareApplication/gcc-arm-none-eabi/bin # 添加gcc-arm-none-eabi路径
  66. # The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
  67. # either it can be added to the PATH environment variable.
  68. ifdef GCC_PATH
  69. CC = $(GCC_PATH)/$(PREFIX)gcc
  70. AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
  71. CP = $(GCC_PATH)/$(PREFIX)objcopy
  72. SZ = $(GCC_PATH)/$(PREFIX)size
  73. else
  74. CC = $(PREFIX)gcc
  75. AS = $(PREFIX)gcc -x assembler-with-cpp
  76. CP = $(PREFIX)objcopy
  77. SZ = $(PREFIX)size
  78. endif
  79. HEX = $(CP) -O ihex
  80. BIN = $(CP) -O binary -S
  81. #######################################
  82. # CFLAGS
  83. #######################################
  84. # cpu
  85. CPU = -mcpu=cortex-m4

  86. # fpu
  87. FPU = -mfpu=fpv4-sp-d16

  88. # float-abi
  89. FLOAT-ABI = -mfloat-abi=hard

  90. # mcu
  91. MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

  92. # macros for gcc
  93. # AS defines
  94. AS_DEFS =

  95. # C defines
  96. C_DEFS = \
  97. -DAPM32F411 \
  98. -DAPM32F411_TINY


  99. # AS includes
  100. AS_INCLUDES =

  101. # C includes
  102. C_INCLUDES = \
  103. -ILibraries/APM32F4xx_StdPeriphDriver/inc \
  104. -ILibraries/Device/Geehy/APM32F4xx/Include \
  105. -ILibraries/CMSIS/Include \
  106. -IBoards \
  107. -IApplication/Include \


  108. # compile gcc flags
  109. ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

  110. CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

  111. ifeq ($(DEBUG), 1)
  112. CFLAGS += -g -gdwarf-2
  113. endif


  114. # Generate dependency information
  115. CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"


  116. #######################################
  117. # LDFLAGS
  118. #######################################
  119. # link script
  120. LDSCRIPT = Tools/APM32F4xxxE_FLASH.ld

  121. # libraries
  122. LIBS = -lc -lm -lnosys
  123. LIBDIR =
  124. LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

  125. # default action: build all
  126. all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

  127. #######################################
  128. # build the application
  129. #######################################
  130. # list of objects
  131. OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
  132. vpath %.c $(sort $(dir $(C_SOURCES)))
  133. # list of ASM program objects
  134. OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
  135. vpath %.s $(sort $(dir $(ASM_SOURCES)))

  136. $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
  137. [url=home.php?mod=space&uid=3148]@echo[/url] "[CC]   lt;"
  138. @$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) lt; -o $@

  139. $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
  140. @echo "[AS]   lt;"
  141. @$(AS) -c $(CFLAGS) lt; -o $@

  142. $(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
  143. @echo "[HEX]   lt; -> $@"
  144. @$(CC) $(OBJECTS) $(LDFLAGS) -o $@
  145. @$(SZ) $@

  146. $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
  147. @echo "[HEX]   lt; -> $@"
  148. @$(HEX) lt; $@

  149. $(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
  150. @echo "[BIN]   lt; -> $@"
  151. @$(BIN) lt; $@

  152. $(BUILD_DIR):
  153. @mkdir $@
  154. #######################################
  155. # flash
  156. #######################################
  157. flash: $(BUILD_DIR)/$(TARGET).bin
  158. pyocd erase -c -t apm32f411ve --config pyocd.yaml
  159. pyocd load output/$(TARGET).hex -t apm32f411ve --config pyocd.yaml
  160. #pyocd load -e sector -t apm32f411vc $(BUILD_DIR)/$(TARGET).bin
  161. #pyocd flash --erase chip --target apm32f411ve --pack=Geehy.APM32F4xx_DFP.1.0.4.pack $(BUILD_DIR)/$(TARGET).hex
  162. #######################################
  163. # clean up
  164. #######################################
  165. clean:
  166. -del /q $(BUILD_DIR)   # del windows系统删除命令
  167. #######################################
  168. # dependencies
  169. #######################################
  170. -include $(wildcard $(BUILD_DIR)/*.d)

  171. # *** EOF ***

  • 编译
    执行make命令
    apm32-gcc11.jpg
    4.Linux环境搭建
    • 安装GNU Arm Embedded Toolchain交叉编译器
      进入arm开发者官网,往下滑动选择下载解压可用的ZIP压缩包文件 ,选择linux版本
      也可以直接在linux系统中下载,这里使用的Ubuntu 22.04
      sudo apt-get install gcc-arm-none-eabi
      下载完成查看
      arm-none-eabi-gcc -v
      apm32-gcc-00.jpg
    • 修改Makefile
      注释GCC_PATH
      apm32-gcc-01.jpg
      修改 clean命令
      apm32-gcc-02.jpg
      make
      apm32-gcc-03.jpg


5.下载程序
  • 注意 printf重定向

      
  1. /*!
  2. * [url=home.php?mod=space&uid=247401]@brief[/url]       Redirect C Library function printf to serial port.
  3. *             After Redirection, you can use printf function.
  4. *
  5. * @param       ch: The characters that need to be send.
  6. *
  7. * @retval     The characters that need to be send.
  8. *
  9. * @note
  10. */
  11. int __io_putchar(int ch)
  12. {
  13.    /* send a byte of data to the serial port */
  14.    USART_TxData(DEBUG_USART, ch);

  15.    /* wait for the data to be send */
  16.    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

  17.    return ch;
  18. }

  19. /*!
  20. * @brief       Redirect C Library function printf to serial port.
  21. *             After Redirection, you can use printf function.
  22. *
  23. * @param       file: Meaningless in this function.
  24. *
  25. * @param       *ptr: Buffer pointer for data to be sent.
  26. *
  27. * @param       len: Length of data to be sent.
  28. *
  29. * @retval     The characters that need to be send.
  30. *
  31. * @note
  32. */
  33. int _write(int file, char* ptr, int len)
  34. {
  35.    int i;
  36.    for (i = 0; i < len; i++)
  37.   {
  38.        __io_putchar(*ptr++);
  39.   }

  40.    return len;
  41. }

  • 测试结果
    pyocd daplink 下载
    apm32-gcc13.jpg
  • apm32-gcc12.jpg

星辰大海不退缩 发表于 2024-6-22 20:47 | 显示全部楼层
Windows环境搭建GNU Arm Embedded Toolchain交叉编译器安装应该注意哪些事项,直接按照流程有时候会报错
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

80

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部