本帖最后由 xhackerustc 于 2024-5-5 20:08 编辑
本人习惯于linux下做开发,平时的工作流程是vim写代码,make/cmake等工具配置gcc/clang工具链编译,uboot或openocd/pyocd烧录。顺着【AT-START-F405测评】linux下pyocd+cmake环境搭建并点灯+printf uart输出组建开发环境已经非常方便,下面说说APM32f411V特别的地方或者说需要在已有linux开发环境基础上再做什么即可搭建用于APM32F411V的开发环境。
pyocd的适用于APM32F411V的pack的安装
pyocd pack find apm32f411
上述命令目前是返回“找不到pack的”,但是再缩小范围比如apm32f可以找到apm32f407之类的。
0000716 W No matching devices. Please make sure the pack index is up to date. [pack_cmd]
这说明APM32F411比较新,官方还未上传pack到Keil,那么就需要手动下载pack包并管理。笔者在apm官网下载到了SDK压缩包,解压SDK后其中Package目录里就有名为Geehy.APM32F4xx_DFP.1.0.4.pack文件,把它拷贝到~/.local/share/cmsis-pack-manager/apm32/即可。
在SDK解压top目录中写一份pyocd.yaml告诉pyocd到哪里找pack文件:
pack:
- ~/.local/share/cmsis-pack-manager/apm32/Geehy.APM32F4xx_DFP.1.0.4.pack
这时把板子swd调试器的usb typec接上电脑,如下命令试试:
pyocd显示lockup,猜测大概率是由于板子未烧录任何固件导致的,先停下cpu打印下cpu寄存器看看
看看lr、rN、pc和xpsr这些寄存器的值基本上肯定是板子默认没固件的。不过这也说明pyocd可以和板子交互了
Makefile模板的编写
这次咱们选直接写Makefile。观察sdk目录,可以发现:驱动都在Libraries/APM32F4xx_StdPeriphDriver/目录中,Boards/Board_APM32F411_TINY/是板级资源定义和适配,而Libraries/Device/Geehy/APM32F4xx/Source/gcc目录是启动汇编代码和ld脚本,Examples目录是示例代码。这次咱们就以Examples/GPIO/GPIO_Toggle/为基础来点灯吧。
有了以上观察后书写完整Makefile如下:
######################################
# target
######################################
TARGET = apm32f411
######################################
# building variables
######################################
# debug build?
DEBUG = 0
# optimization for size, enable lto
OPT = -Os -flto
#######################################
# paths
#######################################
# Build path
BUILD_DIR = build
######################################
# source
######################################
# C sources
C_SOURCES = $(wildcard Libraries/APM32F4xx_StdPeriphDriver/src/*.c)
C_SOURCES += $(wildcard Boards/Board_APM32F411_TINY/src/*.c)
C_SOURCES += $(wildcard Examples/GPIO/GPIO_Toggle/Source/*.c)
# ASM sources
ASM_SOURCES = Libraries/Device/Geehy/APM32F4xx/Source/gcc/startup_apm32f411.S
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES = -ILibraries/APM32F4xx_StdPeriphDriver/inc
C_INCLUDES += -ILibraries/Device/Geehy/APM32F4xx/Include
C_INCLUDES += -ILibraries/CMSIS/Include/
C_INCLUDES += -IBoards/
C_INCLUDES += -IBoards/Board_APM32F411_TINY/inc/
C_INCLUDES += -IExamples/GPIO/GPIO_Toggle/Include
C_DEFS := -DAPM32F411 -DAPM32F411_TINY
#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mthumb -mcpu=cortex-m4
FPU = -mfloat-abi=hard
FLOAT-ABI = -mfloat-abi=hard
# mcu
MCU = $(CPU) $(FPU) $(FLOAT-ABI)
# compile gcc flags
ASFLAGS = $(MCU) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_INCLUDES) $(OPT) $(C_DEFS) -Wall -fdata-sections -ffunction-sections
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = Libraries/Device/Geehy/APM32F4xx/Source/gcc/APM32F4xxxE_FLASH.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -fsigned-char -ffunction-sections -fdata-sections -Wunused -Wuninitialized -T $(LDSCRIPT) -Wl,--gc-sections,--print-memory-usage,-Map=$(BUILD_DIR)/$(TARGET).map --specs=nano.specs $(LIBS) -flto -fuse-linker-plugin
# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.S=.o)))
vpath %.S $(sort $(dir $(ASM_SOURCES)))
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(CC) -c $(CFLAGS) [ DISCUZ_CODE_49 ]lt; -o $@
$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) [ DISCUZ_CODE_49 ]lt; -o $@
#$(LUAOBJECTS) $(OBJECTS)
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
$(SZ) $@
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) [ DISCUZ_CODE_49 ]lt; $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) [ DISCUZ_CODE_49 ]lt; $@
$(BUILD_DIR):
mkdir $@
#######################################
# Program
#######################################
flash: $(BUILD_DIR)/$(TARGET).bin
pyocd load -e sector -t apm32f411vc $(BUILD_DIR)/$(TARGET).bin
#######################################
# clean up
#######################################
clean:
-rm -fR $(BUILD_DIR)
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
编译过程发现sdk的一些问题
1.源码中apm32f4xx_sdio.h写成了apm32f4Xx_sdio.h,apm32f4xx_rcm.h写成了apm32f4Xx_rcm.h
对于区分文件大小写的文件系统来说编译会报错,修正补丁如下:
diff --git a/Boards/Board_APM32F411_TINY/inc/Board_APM32F411_TINY.h b/Boards/Board_APM32F411_TINY/inc/Board_APM32F411_TINY.h
index a4aac67..197b89f 100644
--- a/Boards/Board_APM32F411_TINY/inc/Board_APM32F411_TINY.h
+++ b/Boards/Board_APM32F411_TINY/inc/Board_APM32F411_TINY.h
@[url=home.php?mod=space&uid=72445]@[/url] -40,7 +40,7 @@
#include "apm32f4xx_misc.h"
#include "apm32f4xx_syscfg.h"
#include "apm32f4xx_dma.h"
-#include "apm32f4Xx_sdio.h"
+#include "apm32f4xx_sdio.h"
#include "apm32f4xx_dci.h"
/** @addtogroup Board
diff --git a/Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_sdio.c b/Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_sdio.c
index 8feb411..378dc5b 100644
--- a/Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_sdio.c
+++ b/Libraries/APM32F4xx_StdPeriphDriver/src/apm32f4xx_sdio.c
@[url=home.php?mod=space&uid=72445]@[/url] -23,8 +23,8 @@
* and limitations under the License.
*/
-#include "apm32f4Xx_sdio.h"
-#include "apm32f4Xx_rcm.h"
+#include "apm32f4xx_sdio.h"
+#include "apm32f4xx_rcm.h"
/** @addtogroup APM32F4xx_StdPeriphDriver
@{
2. 目录中"\"与"/"
“/"win和 *nix系统都能用,而"\"只有win能识别,所以建议补丁如下:
diff --git a/Boards/Board.h b/Boards/Board.h
index 87a4b77..7e026e9 100644
--- a/Boards/Board.h
+++ b/Boards/Board.h
@[url=home.php?mod=space&uid=72445]@[/url] -29,16 +29,16 @@
/* Includes */
#if defined (APM32F407_MINI)
-#include "Board_APM32F407_MINI\inc\Board_APM32F407_MINI.h"
+#include "Board_APM32F407_MINI/inc/Board_APM32F407_MINI.h"^M
#elif defined (APM32F407_ELE_HUETB)
-#include "Board_APM32F407_ELE_HUETB\inc\Board_APM32F407_ELE_HUETB.h"
+#include "Board_APM32F407_ELE_HUETB/inc/Board_APM32F407_ELE_HUETB.h"^M
#elif defined (APM32F407_TINY)
-#include "Board_APM32F407_TINY\inc\Board_APM32F407_TINY.h"
+#include "Board_APM32F407_TINY/inc/Board_APM32F407_TINY.h"^M
#elif defined (APM32F411_TINY)
-#include "Board_APM32F411_TINY\inc\Board_APM32F411_TINY.h"
+#include "Board_APM32F411_TINY/inc/Board_APM32F411_TINY.h"^M
点灯代码&串口打印
Examples/GPIO/GPIO_Toggle/已经能够点灯,不过笔者想同时试下串口printf打印,修改补丁如下:
diff --git a/Examples/GPIO/GPIO_Toggle/Source/main.c b/Examples/GPIO/GPIO_Toggle/Source/main.c
index cc572ca..eadafc2 100644
--- a/Examples/GPIO/GPIO_Toggle/Source/main.c
+++ b/Examples/GPIO/GPIO_Toggle/Source/main.c
@[url=home.php?mod=space&uid=72445]@[/url] -63,13 +63,16 @@ int main(void)
{
APM_LEDInit(LED2);
APM_LEDInit(LED3);
+ USART1_Init(115200);^M
/* Init delay function */
Delay_Init();
while (1)
{
Delay_ms(500);
+ printf("Toggle LED2\n");^M
APM_LEDToggle(LED2);
+ printf("Toggle LED3\n");^M
APM_LEDToggle(LED3);
}
}
编译&烧录
make即可编译非常方便,烧录用如下命令:
pyocd load -e sector -t apm32f411ve build/apm32f411.bin
烧录完后板子重启,两颗led开始闪烁,并且swd调试器的cdc串口有打印输出
|