00. 目录
01. 概述
本文用于描述 PY32 微控制器 GCC 编译环境的安装以及使用。通过使用 VSCode 软件实现编辑功能;通过 gcc-arm-none-eabi 软件实现编译功能;通过 CooFlash 软件和 PY-LINK 仿真器、JFlash 软件和 J-Link 仿真器实现下载烧录功能。
02. 安装 GCC
下载:gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.rar
下载:GNU Arm Embedded Toolchain Downloads
2.1 解压gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
deng@local:~/tools$ tar -xjvf gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
2.2 剪切gcc-arm-none-eabi-10.3-2021.10到/var/opt目录中
deng@local:~/tools$ sudo mv gcc-arm-none-eabi-10.3-2021.10 /var/opt/
03. Makefile文件
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Tue Dec 06 16:10:07 CST 2022]
##########################################################################################################################
# ------------------------------------------------
# Generic Makefile (based on gcc)
#
# ChangeLog :
# 2017-02-10 - Several enhancements + project update mode
# 2015-07-22 - first version
# ------------------------------------------------
######################################
# target
######################################
TARGET = PY32F002B_STK
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og
#######################################
# paths
#######################################
# Build path
BUILD_DIR = bin
######################################
# source
######################################
# C sources
C_SOURCES = \
../Src/main.c \
../Src/py32f002b_it.c \
../Src/system_py32f002b.c \
../../../Drivers/PY32F002B_HAL_Driver/Src/py32f002b_ll_utils.c
# ASM sources
ASM_SOURCES = \
startup_py32f002bxx.s
#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
GCC_PATH = /var/opt/gcc-arm-none-eabi-10.3-2021.10/bin
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m0plus
# fpu
# NONE for Cortex-M0/M0+/M3
# float-abi
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# macros for gcc
# AS defines
AS_DEFS =
# C defines
C_DEFS = \
-DUSE_FULL_LL_DRIVER \
-DPY32F002Bx5
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES = \
-I../Inc \
-I../../../Drivers/BSP/PY32F002Bxx_Start_Kit \
-I../../../Drivers/PY32F002B_HAL_Driver/Inc \
-I../../../Drivers/CMSIS/Device/PY32F0xx/Include \
-I../../../Drivers/CMSIS/Include
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -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 = py32f002bx5.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
# 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) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
$(SZ) $@
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@
$(BUILD_DIR):
mkdir $@
#######################################
# clean up
#######################################
clean:
-rm -fR $(BUILD_DIR)
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF ***
04. 链接文件
/*
******************************************************************************
**
** File : LinkerScript.ld
**
** Author : Puya_HC
**
** Abstract : Linker script for PY32F002Bx5 series
** 24Kbytes FLASH and 3Kbytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used.
**
** Target : Puya PY32
**
** Distribution: The file is distributed “as is,” without any warranty
** of any kind.
**
*****************************************************************************
** @attention
**
** <h2><center>© COPYRIGHT(c) 2019 Puya Semiconductor</center></h2>
**
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
** 1. Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
** 3. Neither the name of Puya Semiconductor nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*****************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x000; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 3K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 24K
}
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH
/* Constant data goes into FLASH */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH
/* used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections goes into RAM, load LMA copy after code */
.data :
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM AT> FLASH
/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAM
/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
}
05. 编译
5.1 先解压 PY32F002B_Firmware_V0.0.2.rar到Ubuntu中
5.2 修改Makefile和链接脚本如上述所示
5.3 编译
deng@local:~/PY32F002B_Firmware_V0.0.2/Templates/PY32F002Bxx_Templates_LL/Makefile$ pwd
/home/deng/PY32F002B_Firmware_V0.0.2/Templates/PY32F002Bxx_Templates_LL/Makefile
deng@local:~/PY32F002B_Firmware_V0.0.2/Templates/PY32F002Bxx_Templates_LL/Makefile$ make
mkdir bin
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb -DUSE_FULL_LL_DRIVER -DPY32F002Bx5 -I../Inc -I../../../Drivers/BSP/PY32F002Bxx_Start_Kit -I../../../Drivers/PY32F002B_HAL_Driver/Inc -I../../../Drivers/CMSIS/Device/PY32F0xx/Include -I../../../Drivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"bin/main.d" -Wa,-a,-ad,-alms=bin/main.lst ../Src/main.c -o bin/main.o
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb -DUSE_FULL_LL_DRIVER -DPY32F002Bx5 -I../Inc -I../../../Drivers/BSP/PY32F002Bxx_Start_Kit -I../../../Drivers/PY32F002B_HAL_Driver/Inc -I../../../Drivers/CMSIS/Device/PY32F0xx/Include -I../../../Drivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"bin/py32f002b_it.d" -Wa,-a,-ad,-alms=bin/py32f002b_it.lst ../Src/py32f002b_it.c -o bin/py32f002b_it.o
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb -DUSE_FULL_LL_DRIVER -DPY32F002Bx5 -I../Inc -I../../../Drivers/BSP/PY32F002Bxx_Start_Kit -I../../../Drivers/PY32F002B_HAL_Driver/Inc -I../../../Drivers/CMSIS/Device/PY32F0xx/Include -I../../../Drivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"bin/system_py32f002b.d" -Wa,-a,-ad,-alms=bin/system_py32f002b.lst ../Src/system_py32f002b.c -o bin/system_py32f002b.o
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb -DUSE_FULL_LL_DRIVER -DPY32F002Bx5 -I../Inc -I../../../Drivers/BSP/PY32F002Bxx_Start_Kit -I../../../Drivers/PY32F002B_HAL_Driver/Inc -I../../../Drivers/CMSIS/Device/PY32F0xx/Include -I../../../Drivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"bin/py32f002b_ll_utils.d" -Wa,-a,-ad,-alms=bin/py32f002b_ll_utils.lst ../../../Drivers/PY32F002B_HAL_Driver/Src/py32f002b_ll_utils.c -o bin/py32f002b_ll_utils.o
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc -x assembler-with-cpp -c -mcpu=cortex-m0plus -mthumb -DUSE_FULL_LL_DRIVER -DPY32F002Bx5 -I../Inc -I../../../Drivers/BSP/PY32F002Bxx_Start_Kit -I../../../Drivers/PY32F002B_HAL_Driver/Inc -I../../../Drivers/CMSIS/Device/PY32F0xx/Include -I../../../Drivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"bin/startup_py32f002bxx.d" startup_py32f002bxx.s -o bin/startup_py32f002bxx.o
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc bin/main.o bin/py32f002b_it.o bin/system_py32f002b.o bin/py32f002b_ll_utils.o bin/startup_py32f002bxx.o -mcpu=cortex-m0plus -mthumb -specs=nano.specs -Tpy32f002bx5.ld -lc -lm -lnosys -Wl,-Map=bin/PY32F002B_STK.map,--cref -Wl,--gc-sections -o bin/PY32F002B_STK.elf
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-size bin/PY32F002B_STK.elf
text data bss dec hex filename
1024 12 1052 2088 828 bin/PY32F002B_STK.elf
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-objcopy -O ihex bin/PY32F002B_STK.elf bin/PY32F002B_STK.hex
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-objcopy -O binary -S bin/PY32F002B_STK.elf bin/PY32F002B_STK.bin
deng@local:~/PY32F002B_Firmware_V0.0.2/Templates/PY32F002Bxx_Templates_LL/Makefile$
5.4 生成了bin文件和hex文件
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-objcopy -O ihex bin/PY32F002B_STK.elf bin/PY32F002B_STK.hex
/var/opt/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-objcopy -O binary -S bin/PY32F002B_STK.elf bin/PY32F002B_STK.bin
5.5 使用烧录工具烧录到开发板即可